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.
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.
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")
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.
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.
18.118.200.161