查看原文
其他

ggplot2 如何在不同分面添加不同文字

JunJunLab 老俊俊的生信笔记 2023-09-04


雨下一整晚

1引言

在不同分面添加不同文字确实是有需求的,这里整理了一下 stack overflow 上面的一些解决方案分享给大家。

参考链接:

https://stackoverflow.com/questions/11889625/annotating-text-on-individual-facet-in-ggplot2

2分析

我们直接添加的话默认会给每个分面添加相同的内容:

library(ggplot2)

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(. ~ cyl)
p <- p + annotate("text", label = "Test", size = 4, x = 15, y = 5)

print(p)

解决方案一:

构建一个数据,指定其中一个分面的组为因子水平:

ann_text <- data.frame(mpg = 15,wt = 5,lab = "Text",
                       cyl = factor(8,levels = c("4","6","8")))

p + geom_text(data = ann_text,label = "Text")

可以看到只在 8 的分面添加了文字。

解决方案二:

直接添加一列分列组别的列,可以在每个分面添加对应的文字信息:

library(ggplot2)

p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  facet_grid(. ~ cyl) +
  theme(panel.spacing = unit(1"lines"))
p
dat_text <- data.frame(
  label = c("4 cylinders""6 cylinders""8 cylinders"),
  cyl   = c(468)
)
p + geom_text(
  data    = dat_text,
  mapping = aes(x = -Inf, y = -Inf, label = label),
  hjust   = -0.1,
  vjust   = -1
)

指定不同的位置:

dat_text <- data.frame(
  label = c("4 cylinders""6 cylinders""8 cylinders"),
  cyl   = c(468),
  x     = c(2027.525),
  y     = c(444.5)
)

p + geom_text(
  data    = dat_text,
  mapping = aes(x = x, y = y, label = label)
)

可以再添加一列分面分组的信息,进行多分面添加:

dat_text <- data.frame(
  cyl   = c(468468),
  am    = c(000111)
)
dat_text$label <- sprintf(
  "%s, %s cylinders",
  ifelse(dat_text$am == 0"automatic""manual"),
  dat_text$cyl
)
p +
  facet_grid(am ~ cyl) +
  geom_text(
    size    = 5,
    data    = dat_text,
    mapping = aes(x = Inf, y = Inf, label = label),
    hjust   = 1.05,
    vjust   = 1.5
  )

3添加图标签

这里还可以使用 egg 包里的 tag_facet()tag_facet_outside() 给图添加标签用于达到发表文章的需求:

library(ggplot2)

p <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point() +
  facet_grid(. ~ am) +
  theme_bw(base_size = 12)

# install.packages('egg', dependencies = TRUE)
library(egg)

添加:

tag_facet(p)

添加到右边:

tag_facet(p, x = Inf, y = Inf,
          hjust = 1.5,
          tag_pool = as.roman(1:nlevels(factor(mtcars$am))))

添加到左下角:

tag_facet(p,
          x = -Inf, y = -Inf,
          vjust = -1,
          open = "", close = ")",
          tag_pool = LETTERS)

指定自己的标签:

my_tag <- c("i) 4 cylinders""ii) 6 cyls")
tag_facet(p,
          x = -Inf, y = -Inf,
          vjust = -1, hjust = -0.25,
          open = "", close = "",
          fontface = 4,
          size = 5,
          family = "serif",
          tag_pool = my_tag)

添加到外面:

p2 <- ggplot(mtcars, aes(qsec, mpg)) +
  geom_point() +
  facet_grid(cyl ~ am, switch = 'y') +
  theme_bw(base_size = 12) +
  theme(strip.placement = 'outside')

tag_facet_outside(p2)

也可以使用 stickylabeller 包来给 分面标题 添加不同样式的文字:

- `.n` numbers the facets numerically: `"1"`, `"2"`, `"3"`...
- `.l` numbers the facets using lowercase letters: `"a"`, `"b"`, `"c"`...
- `.L` numbers the facets using uppercase letters: `"A"`, `"B"`, `"C"`...
- `.r` numbers the facets using lowercase Roman numerals: `"i"`, `"ii"`, `"iii"`...
- `.R` numbers the facets using uppercase Roman numerals: `"I"`, `"II"`, `"III"`...

# devtools::install_github("rensa/stickylabeller")
library(stickylabeller)

ggplot(mtcars, aes(qsec, mpg)) +
  geom_point() +
  facet_wrap(. ~ am,
             labeller = label_glue('({.l}) am = {am}')) +
  theme_bw(base_size = 12)

4讨论

这里讨论你的 文本数据的 x,y 是否得跟你的主图数据的 x,y 列名是否一致:

ann_text<-data.frame(mpg=c(25,15),wt=c(3,5),cyl=c(6,8),label=c("Label 1","Label 2"))

ann_text
>  mpg wt cyl  label
>  25  3   6   Label 1
>  15  5   8   Label 2

p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(. ~ factor(cyl))
p + geom_text(data = ann_text,label=ann_text$label)

你可以把图层的 继承参数 关闭 inherit.aes = FALSE 来使用自己的列名:

Data_combined %>%
  ggplot(aes(x=Data_1,y=Data_2,color=Vars)) +
  geom_point() +
  geom_smooth(method="lm",se=FALSE) +
  geom_text(data=label_df,aes(x=x,y=y,label=rlabel),inherit.aes = FALSE) +
  geom_text(data=label_df,aes(x=x,y=y-10,label=plabel),inherit.aes = FALSE) +
    facet_wrap(~ Vars)

和原来的数据保持一致可以减少发送错误的问题。





  老俊俊生信交流群 (微信交流群需收取20元入群费用(防止骗子和便于管理))



老俊俊微信:


知识星球:



今天的分享就到这里了,敬请期待下一篇!

最后欢迎大家分享转发,您的点赞是对我的鼓励肯定

如果觉得对您帮助很大,赏杯快乐水喝喝吧!




  





ggplot2 如何在不同分面添加不同图形

jjAnno 优雅的帮你添加注释

grid 给图添加图片

富集图形添加注释美化

R 中的控制结构

ggpolar 绘制极坐标图形

transPlotR 优雅的绘制基因转录本结构

ggarchery 添加个性化箭头

scRNAtoolVis 0.0.3 版本更新

customize 你的 GSEA 图

◀...

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存