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

  • R Graphs Cookbook Second Edition
  • Jaynal Abedin Hrishi V. Mittal
  • 418字
  • 2021-08-05 17:30:32

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.

主站蜘蛛池模板: 醴陵市| 镇远县| 裕民县| 盐亭县| 横峰县| 肃宁县| 牡丹江市| 潜山县| 文山县| 东宁县| 托里县| 曲沃县| 兴义市| 宣武区| 固始县| 宁陕县| 呼伦贝尔市| 屯留县| 沙雅县| 新邵县| 苗栗市| 驻马店市| 河东区| 博野县| 上饶市| 南郑县| 吉木萨尔县| 福泉市| 简阳市| 八宿县| 集贤县| 汾西县| 赞皇县| 津市市| 宣城市| 仙居县| 根河市| 孙吴县| 霍林郭勒市| 岳普湖县| 渭南市|