官术网_书友最值得收藏!

Setting colors for text elements – axis annotations, labels, plot titles, and legends

Axis annotations are the numerical or text values placed beside tick marks on an axis. Axis labels are the names or titles of axes, which tell the reader what the values on a particular axis represent. In this recipe, you will learn how to set the colors for these elements and legends.

Getting ready

All you need to try out this recipe is to run R and type the recipe in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.

How to do it...

Let's say that we want to make the axis value annotations black, the labels of the axes gray, and the plot title dark blue. For this, you should use the following code:

plot(rnorm(100), 
main="Plot Title",
col.axis="blue",
col.lab="red",
col.main="darkblue")

How it works...

Colors for axis annotations, labels, and plot titles can be set either using the par() command before creating the graph or in the graph command, such as plot() itself. The arguments for setting the colors for axis annotations, labels, and plot titles are col.axis, col.lab, and col.main, respectively.

These are similar to the col argument and take names of colors or hex codes as values, but they do not take a vector of more than one color.

There's more...

If we use the par() command, the difference is that par() will apply these settings to every subsequent graph until it is reset either by specifying the settings again or starting a new graphics device:

par(col.axis="black",
col.lab="#444444",
col.main="darkblue")

plot(rnorm(100),main="plot")

The col.axis argument can also be passed to the axis() function, which is useful for creating a custom axis if you do not want to use the default axis. The col.lab argument does not work with axis() and must be specified in par() or the main graph function such as plot() or barplot().

The col.main argument can also be passed to the title() function, which is useful for adding a custom plot title if you do not want to use the default title:

title("Sales Figures for 2010", col.main="blue")

Axis labels can also be specified with title():

title(xlab="Month",ylab="Sales",col.lab="red")

This is handy because you can specify two different colors for the x and y axes:

title(xlab="X axis",col.lab="red")
title(ylab="Y axis",col.lab="blue")

When setting the axis titles with the title() command, we must set xlab and ylab to the "" empty strings in the original plot command in order to avoid overlapping titles.

主站蜘蛛池模板: 方正县| 襄城县| 怀宁县| 渑池县| 白城市| 锡林浩特市| 沿河| 宁强县| 东安县| 南宁市| 镇平县| 桂东县| 额济纳旗| 离岛区| 绵阳市| 鸡东县| 建水县| 淮北市| 土默特左旗| 上思县| 永清县| 鸡泽县| 惠东县| 惠安县| 丰宁| 岗巴县| 平顺县| 海安县| 衡南县| 理塘县| 岫岩| 朝阳区| 望奎县| 大冶市| 星座| 杭州市| 汽车| 噶尔县| 遂宁市| 玉屏| 西乌|