Quick guide to pch symbols in R

R pch symbols https://www.benjaminbell.co.uk

A quick guide to pch symbols in R, including: which symbols are available, how to use them in plots and how to style them by changing colours, size, and line widths.

A handy reference for pch!


© 2018 Benjamin Bell. All Rights Reserved. https://www.benjaminbell.co.uk

pch symbols in R

! This guide was written using R version 3.4.2 on Windows 10.

pch ("plotting character") is the graphical parameter for drawing symbols on plots in R. For example, pch=0 would plot the points as squares. There are 26 default symbols which are compatible across all systems and devices.

Default pch symbols

R pch symbols List of the default pch symbols in R, and what they look like when plotted. https://www.benjaminbell.co.uk 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Can't see the image? You need to upgrade your browser.

For symbols 0 to 14, you can change the symbol colour col=, line width lwd= and size cex=. For example:

 plot(1:3, pch=c(0, 4, 12), col=c("red", "blue", "green"), lwd=c(2, 4, 6), cex=c(1, 2, 3))

Results in the following symbols:


Symbols 15 to 18 are "solid" symbols. These have no border so you cannot adjust the line width, but you can still change the size cex= and the colour col=.

plot(1:3, pch=c(15, 16, 17), col=c("red", "blue", "green"), cex=c(1, 2, 3))

Symbol 19 is like symbol 16, except it has a border which you can adjust lwd=, and this may make it appear larger. You can also change the size cex= and colour col=, but you cannot change the background colour (use 21). Symbol 20 is like 19, except it is smaller. So, if you were to plot both symbols, and cex= was set to 2, they would be plotted at different sizes:

plot(1:2, pch=c(19, 20), col="blue", lwd=1, cex=2)

Symbols 21 to 25 are "filled" symbols, so as well as being able to change the line width lwd=, size cex= and line colour col=, you can also change the background colour bg=, making these symbols the most versatile.

plot(1:3, pch=c(21, 22, 23), col=c("red", "orange", "limegreen"), bg=c("skyblue", "yellow", "hotpink"), lwd=c(3, 6, 3), cex=3)

If you were plotting some data, and wanted to set the symbols and/or colour based on a parameter, such as a group, you can do that by specifying the symbols, followed by using the subset option [square brackets] to specify the groups. See the following code as an example:

# Create data
set.seed(2333)
x <- rnorm(12)
y <- rnorm(12)
# Create groups
g <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)

# Plot different symbols depending on the group
plot(x, y, pch=c(1, 18, 25)[g], col=c("red", "green2", "blue")[g], bg="skyblue2", lwd=2, cex=4)
# Add labels
text(x, y, labels=1:12, font=2)

Which would result in the following plot:

© 2018 Benjamin Bell. All Rights Reserved. https://www.benjaminbell.co.uk

ASCII symbols

In addition to the default symbols, you can also plot ASCII characters using pch=32:126.

You can change the colour col= and size cex= of the ASCII character symbols.

32 33 34 35 36 37 38 39 40 41 42
! " # $ % & ' ( ) *
43 44 45 46 47 48 49 50 51 52 53
+ , - . / 0 1 2 3 4 5
54 55 56 57 58 59 60 61 62 63 64
6 7 8 9 : ; < = > ? @
65 66 67 68 69 70 71 72 73 74 75
A B C D E F G H I J K
76 77 78 79 80 81 82 83 84 85 86
L M N O P Q R S T U V
87 88 89 90 91 92 93 94 95 96 97
W X Y Z [ \\ ] ^ _ ` a
98 99 100 101 102 103 104 105 106 107 108
b c d e f g h i j k l
109 110 111 112 113 114 115 116 117 118 119
m n o p q r s t u v w
120 121 122 123 124 125 126
x y z { | } ~
© 2018 Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Other symbols

"Native" symbols, which may be specific to the OS you are using, or your locale (e.g. language/keyboard settings), can be accessed using pch=128:255.

To see a list of the symbols that are available for your system, use the following code in the R console (your results may vary):

pch <- as.character(128:255)
sym <- intToUtf8(pch, multiple=TRUE)
sym

You can also specify a character as the symbol (instead of using numbers). For example, if you wanted your plot points to be represented by the letter a, you could use pch="a". This is case sensitive, so "a" is different to "A".

For example, the following code will plot the symbols as characters:

# Create data
set.seed(2333)
x <- rnorm(12)
y <- rnorm(12)

# Plot symbols as characters
plot(x, y, pch=c("A", "a", "@", "%", "s", "{", "N", "Z", "h", "?", "D", "5"), col=1:12, bg="skyblue2", lwd=2, cex=4)
If you can't see this image, you need to upgrade your browser.

Or, you could set the character symbol automatically based on a parameter in your data, such as labels or groups for your dataset. The "symbol" used will be the first character of the label. See the following code for an example:

# Create data
set.seed(2333)
x <- rnorm(12)
y <- rnorm(12)
# Set labels
m <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

# Plot symbols based on labels
plot(x, y, pch=as.character(m), col=rainbow(12), lwd=2, cex=3)

Thanks for reading this guide! Please leave any comments or questions below.


Ad

© 2018 Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Further reading

A quick guide to line types (lty) - A quick guide to the different line types available in R, and how to use them. [R Graphics]

A quick guide to layout() in R - How to create multi-panel plots and figures using the layout() function. [R Graphics]


No comments:

Post a Comment