Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding arrow symbols in ggplot text in R

I recently found out that it is possible to add greek alphabets and other symbols within the annotate text in ggplot. I am trying to add arrows (upwards and downwards) to my text and I can't seem to find the correct numerical code for it. I tried searching but can't find a list of codes online. The syntax is something like that under label:

label="'test text ' * symbol('\\205')"

\142 gives a beta symbol and \154 gives a gamma symbol. Anyone knows how does this code works? Thanks!

like image 646
Binggg Avatar asked Jun 15 '16 01:06

Binggg


1 Answers

The following works:

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth(aes(colour = "loess"), method = "loess", se = FALSE) +
  geom_smooth(aes(colour = "lm"), method = "lm", se = FALSE) +
  labs(colour = "Method") +
  annotate("text", x = 3.5, y = 35, label = sprintf('\u2191')) +
  geom_curve(aes(x = 4, y = 30, xend = 3.5, yend = 34), 
             colour = "#FF0000", 
             size=0.5, 
             curvature = -0.2,
             arrow = arrow(length = unit(0.03, "npc"))) +
  geom_label(aes(x = 4, y = 31, label = "Here is the\nUnicode symbol"), 
             hjust = 0, 
             vjust = 0.5, 
             colour = "#FAAB18", 
             fill = "white", 
             label.size = NA, 
             family="Helvetica", 
             size = 6)

Unicode for arrows from here

Here is the resulting plot:

enter image description here

like image 95
espinielli Avatar answered Oct 14 '22 21:10

espinielli