R语言ggplot2作图如何添加特殊字符~一文就够

2021-07-12 15:43:23 浏览数 (1)

之前写过一篇文档来介绍如何给标签添加特殊字符,近来有朋友需要画图正好又遇到更加复杂的需求,索性来一篇文档对此进行归纳总结,后台回复2021-6-5获取本文代码;喜欢的小伙伴欢迎扫描文末二维码加入我的交流群,更多精彩内容为您而准备

之前的方法通过bquote函数进行特殊字符格式的添加,如下所示

代码语言:javascript复制
iris %>% as_tibble() %>% 
  ggplot(aes(Sepal.Length,Sepal.Width,fill=Species)) 
  geom_point(size=4,pch=21,color="white") 
  xlab(bquote(Assimilation (mu~ mol ~CO[2]~ m^-2~s^-1))) 
  theme_classic()

如果我们需要在图中添加特殊字符该如何进行操作,同时给不同字符加以不同颜色又该如何进行设置,ggtext的出现很好的解决了这一问题

通过字符编号添加特殊字符
代码语言:javascript复制
iris %>% as_tibble() %>% 
  ggplot(aes(Sepal.Length,Sepal.Width,fill=Species)) 
  geom_point(size=4,pch=21,color="white") 
  theme_classic() 
  labs(x="Assimilation(u03bc molCO<sub>2</sub>m<sup>-2s</sup>-1)",
       y=NULL) 
  theme(axis.title.x = element_markdown(color="#3B9AB2",vjust=0.5))

可以看到通过u03bc 此字符编号添加了特殊字符,那么有没有其它的方法呢,当然有请继续往下看

代码语言:javascript复制
iris %>% as_tibble() %>% 
  ggplot(aes(Sepal.Length,Sepal.Width,fill=Species)) 
  geom_point(size=4,pch=21,color="white") 
  theme_classic() 
  labs(title="TEST &mu;",
    x="Assimilation(<span style='color:#F98400;'>
    &mu;</span>molCO<sub>2</sub>m<sup>-2s</sup>-1)",
    y="<span style='color:#F98400;'>&alpha;&beta;</span>") 
  theme(plot.title=element_markdown(),
    axis.title.x = element_markdown(color="#3B9AB2"),
    axis.title.y = element_markdown())

此图通过另外一种方式添加了特殊字符,也对部分字符颜色进行了定义,那同样我们也可以对字体大小等进行定义

下面展示一个小细节,此处含有端倪

可以看到我们将Y轴文本移至了右边,因此标签也需要定义到右边axis.title.y.right

代码语言:javascript复制
ggplot(data.frame(x = c(-5, 5)), aes(x))  
  stat_function(fun = ~ .x*.x) 
  labs(
    x = "independent variable *x*",
    y = "dependent variable *y* = *x*<sup>2</sup>"
  )  
  scale_y_continuous(position = "right")  
  theme(
    axis.title = element_text(color = "#0072B2",
                              size = rel(1)),
    axis.title.x = element_markdown(),
    axis.title.y.right = element_markdown()
  )

经上面的介绍特殊字符的添加已经变得非常容易了,但是有没有发现所展示的案例基本都是在图形外部添加字符,有时我们也需要在图内部添加一些特殊字符,因此这一部分也很重要

内部添加特殊字符

定义字符位置信息

代码语言:javascript复制
df <- tibble(
  label = c("SiO<sub>4</sub><sup>4-",
            "NO<sup>2-","&mu;",
            "&alpha;&beta;","R<sup>2</sup>=0.001",
            "p_value< 2.1e<sup>-16</sup>"),
  x = c(4.8,4.8,8,7,6.5,7.5),
  y = c(4.1,4.3,2.3,4.2,4,4),
  angle=c(0,10,0,0,0,0),
  color= c("black","blue","black","red","green","black"))
添加文本
代码语言:javascript复制
iris %>% as_tibble() %>% 
  ggplot(aes(Sepal.Length,Sepal.Width,fill=Species)) 
  geom_point(size=4,pch=21,color="white") 
  theme_classic() 
  labs(x="Assimilation(u03bc molCO<sub>2</sub>m<sup>-2s</sup>-1)",
       y=NULL) 
  theme(axis.title.x = element_markdown(color="#3B9AB2",vjust=0.5)) 
  geom_richtext(data=df,aes(x,y,label=label,angle=angle,color=color),
                fill=NA,label.color=NA,show.legend = F)
文本添加边框
代码语言:javascript复制
iris %>% as_tibble() %>% 
  ggplot(aes(Sepal.Length,Sepal.Width,fill=Species)) 
  geom_point(size=4,pch=21,color="white",show.legend = F) 
  theme_classic() 
  labs(x="Assimilation(u03bc molCO<sub>2</sub>m<sup>-2s</sup>-1)",
       y=NULL) 
  theme(axis.title.x = element_markdown(color="#3B9AB2",vjust=0.5)) 
  geom_richtext(data=df,aes(x,y,label=label,angle=angle),
                fill=NA,show.legend = F)
添加标题
代码语言:javascript复制
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species))  
  geom_point(size = 3)  
  scale_color_manual(
    name = NULL,
    values = c(setosa = "#0072B2", virginica = "#009E73", versicolor = "#D55E00"),
    labels = c(
      setosa = "<i style='color:#0072B2'>I. setosa  &mu; </i>",
      virginica = "<i style='color:#009E73'>I. virginica  &mu; </i>",
      versicolor = "<i style='color:#D55E00'>I. versicolor  &mu; </i>")
  )  
  labs(
    title = "**Fisher's *Iris* dataset(test unicode symbol: &mu;)**  
    <span style='font-size:11pt'>Sepal width vs. sepal length for three *Iris*
    species  &mu; </span>")  
  theme_minimal()  
  theme(
    plot.title = element_markdown(lineheight = 1.1),
    legend.text = element_markdown(size = 11),
    axis.title.x = element_markdown(hjust = 0.5),
    axis.title.y = element_markdown(vjust = 0.5))

在上图中我们对部分字体大小及字体类型进行了设置,并进行了换行操作,此处用到了一些markdown语法

给标题更丰富的花样
代码语言:javascript复制
base <- mtcars %>%
  mutate(
    transmission = ifelse(am == 1, "automatic", "manual")
  ) %>%
  ggplot(aes(hp, mpg, color = transmission))  
  geom_point(size = 2)  
  scale_color_manual(
    values = c(automatic = "#0072B2", manual = "#D55E00"),
    guide = "none"
  )  
  labs(
    x = "Horse power",
    y = "Miles per gallon (MPG)",
    title = "Transmission type impacts fuel efficiency<br>
<span style = 'font-size:10pt;'>Miles per gallon (MPG) is on average higher for cars
with <span style = 'color:#0072B2;'>automatic transmission</span> than for cars with
<span style = 'color:#D55E00;'> manual transmission.</span> However, MPG generally
declines with increasing horse power.</span>"
  )  
  theme_bw()   theme(plot.title.position = "plot")

base  
  theme(
    plot.title = element_textbox_simple(
      size = 14, lineheight = 1, padding = margin(0, 0, 5, 0)
    )
  )
给标题添加边框
代码语言:javascript复制
base  
  theme(
    plot.title = element_textbox_simple(
      size = 14, lineheight = 1,
      linetype = 1,
      box.color = "#748696",
      fill = "#F0F7FF",
      r = grid::unit(3, "pt"),
      padding = margin(5, 5, 5, 5),
      margin = margin(0, 0, 10, 0)
    )
  )
控制边框位置
代码语言:javascript复制
base  
  theme(
    plot.title = element_textbox_simple(
      size = 14, lineheight = 1, 
      width = grid::unit(4, "in"),
      hjust = 1, 
      linetype = 1, 
      box.color = "#748696",
      fill = "#F0F7FF", 
      r = grid::unit(3, "pt"), 
      padding = margin(5, 5, 5, 5),
      margin = margin(0, 0, 10, 0)
    )
  )

0 人点赞