Here's a simple example.
library(tidyverse)
dat <- data.frame(x = c(1,2,3,4,5),
y = c(1,2,3,4,5))
ggplot(dat, aes(x, y)) +
geom_point(shape="\u2620", size = 8)
This works perfectly to create skull and crossbones as the shapes, as 2620 is the hex value for this unicode character. I actually want the elephant shape, which has the hex code 1F418.
However, substituting 1F418 for 2620 produces the error message
Error: Can't find shape name: * '8'
Why does the elephant shape not work? How can I get the elephant shape to appear in my plot?
Change point shapes, colors and sizes manually : 1 scale_shape_manual () : to change point shapes. 2 scale_color_manual () : to change point colors. 3 scale_size_manual () : to change the size of points ggplot(df, aes(x=wt, y=mpg, group=cyl)) + geom_point(aes(shape=cyl, color=cyl), size=2)+ ...
Unicode characters. Unicode is a standardized coding scheme for characters of all languages. As of March 2020, there are close to 150,000 different characters with a corresponding unicode designation. To call a unicode symbol in R, you can simply type the corresponding sequence in a quoted string such as “\u03b1” which will print α.
In R, shapes are assigned to different numbers or symbols, so in order to specify the shapes we want for our data points, we need to specify the number associated with the shape that we want. Let’s take a look at the different shapes that we can use:
The lower case \u
escape prefix represents Unicode characters with 16-bit hex values. For 32-bit hex values use the upper case \U
or a surrogate pair (two 16-bit values):
ggplot(dat, aes(x, y)) +
geom_point(shape="\U1F418", size = 8) # is "\U0001f418"
Or:
ggplot(dat, aes(x, y)) +
geom_point(shape="\uD83D\uDC18", size = 8)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With