Adding arrow symbols in ggplot text in R

2019-07-12 17:42发布

问题:

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!

回答1:

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: