- R Graphs Cookbook Second Edition
- Jaynal Abedin Hrishi V. Mittal
- 415字
- 2021-08-05 17:30:32
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.
- 深入理解Spring Cloud與實戰
- Learning Cocos2d-x Game Development
- Effective STL中文版:50條有效使用STL的經驗(雙色)
- Visual Media Processing Using Matlab Beginner's Guide
- 計算機組裝與維護(第3版)
- Rapid BeagleBoard Prototyping with MATLAB and Simulink
- Blender Quick Start Guide
- 從企業級開發到云原生微服務:Spring Boot實戰
- The Machine Learning Workshop
- 零基礎輕松學修電腦主板
- Unreal Development Kit Game Programming with UnrealScript:Beginner's Guide
- 微型計算機原理及應用教程(第2版)
- 51單片機應用開發從入門到精通
- Mastering TensorFlow 1.x
- 精選單片機設計與制作30例