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

Choosing plotting point symbol styles and sizes

In this recipe, we will see how we can adjust the styling of plotting symbols, which is useful and necessary when we plot more than one set of points that represent different groups of data on the same graph.

Getting ready

All you need to try out in 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. We will also use the cityrain.csv example data file that we used in the first chapter. Please read the file into R as follows:

rain<-read.csv("cityrain.csv")

How to do it...

The plotting symbol and size can be set using the pch and cex arguments:

plot(rnorm(100),pch=19,cex=2)

How it works...

The pch argument stands for the plotting character (the symbol). It can take numerical values (usually between 0 and 25) as well as single character values. Each numerical value represents a different symbol. For example, 1 represents circles, 2 represents triangles, 3 represents plus signs, and so on. If we set the value of pch to a character such as * or in inverted commas, then the data points are drawn as that character instead of the default circles.

The size of the plotting symbol is controlled by the cex argument, which takes numerical values starting at 0, giving us the amount by which plotting symbols should be magnified, relative to the default. Note that cex takes relative values (the default is 1). So, the absolute size might vary depending on the defaults of the graphic device in use. For example, the size of plotting symbols with the same cex value might be different for a graph saved as a PNG file versus a graph saved as a PDF.

There's more...

The most common use of pch and cex is when we don't want to use colors to distinguish between different groups of data points. This is often the case in scientific journals that do not accept color images. For example, let's plot the city rainfall data we looked at in Chapter 1, R Graphics, as a set of points instead of lines:

plot(rain$Tokyo,
ylim=c(0,250),
main="Monthly Rainfall in major cities",
xlab="Month of Year",
ylab="Rainfall (mm)",
pch=1)

points(rain$NewYork,pch=2)
points(rain$London,pch=3)
points(rain$Berlin,pch=4)

legend("top",
legend=c("Tokyo","New York","London","Berlin"),
ncol=4,
cex=0.8,
bty="n",
pch=1:4)

See also

We will see more examples of symbol settings later in the book, especially in the next chapter on scatter plots.

主站蜘蛛池模板: 长泰县| 镇平县| 溧阳市| 专栏| 清水县| 贵定县| 梁河县| 察雅县| 江达县| 梁山县| 图片| 竹山县| 襄垣县| 蓬溪县| 丘北县| 江都市| 彰化市| 延吉市| 策勒县| 景东| 年辖:市辖区| 常宁市| 酒泉市| 闻喜县| 望城县| 千阳县| 孟连| 泸西县| 湟源县| 法库县| 吉安县| 繁昌县| 河间市| 客服| 揭东县| 自贡市| 内黄县| 琼结县| 兴安县| 依兰县| 河东区|