Adding arrow symbols in ggplot text in R

旧时模样 提交于 2019-12-12 16:07:58

问题


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:



来源:https://stackoverflow.com/questions/37824742/adding-arrow-symbols-in-ggplot-text-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!