在多个方面注释GGPLOT2

发布于 2025-01-27 07:49:36 字数 1873 浏览 2 评论 0原文

最近,我开始使用GGH4X软件包中的facet_nested函数,我真的很喜欢嵌套轴的外观。我想注释剧情以显示我运行的统计数据。我创建了一个虚拟数据集来说明我的问题。

library(tidyverse)
library(markdown)
library(ggtext)
library(ggh4x)


df <- data.frame(pretreatment = c("10NA", "10NA","10NA", "NT", "NT", "NT"),
                 timepoint = c("0 h", "6 h","6 h", "0 h", "6 h", "6 h"),
                 treatment = c("baseline", "10NA", "NT","baseline", "10NA", "NT"),
                 mean_copy_no = c(1000, 1500, 1200, 600, 700, 400), 
                 sample_id = c(1, 2, 3, 4, 5, 6))

df %>%
  ggplot(aes(x=sample_id, y = mean_copy_no, fill = treatment)) +
  geom_col(colour = "black") +
  facet_nested(.~ pretreatment + timepoint + treatment, scales = "free", nest_line = TRUE, switch = "x") +
  ylim(0,2000) +
  theme_bw() +
  theme(strip.text.x = element_text(size = unit(10, "pt")),
        legend.position = "none",
        axis.title.y = element_markdown(size = unit(13, "pt")),
        axis.text.y = element_text(size = 11),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.text = element_markdown(size = unit(12, "pt")),
        strip.background = element_blank(),
        panel.spacing.x = unit(0.05,"line"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.border = element_blank())

这会生成以下图。 “

现在我的问题是每个栏都位于其自己的方面,而不是全部在一个x轴上(如果您在没有主题的情况下运行代码,它显示出更清楚)。

我绘制了我希望情节的样子。

我想添加线条和恒星以表明显着差异。

我可以添加easilly,但是我正在努力添加线条,我知道这甚至可能是不可能的,因为我正在使用方面来生成情节。我只是想发布这个问题,看看有人在R中是否有任何建议。或者是否有一种无需使用方面就可以实现嵌套外观的方法。

*为了清晰而编辑。

I have recently started using the facet_nested function from the ggh4x package and I really like the look of the nested axis. I would like to annotate the plot to show stats that I have run. I have created a dummy dataset to illustrate my problem.

library(tidyverse)
library(markdown)
library(ggtext)
library(ggh4x)


df <- data.frame(pretreatment = c("10NA", "10NA","10NA", "NT", "NT", "NT"),
                 timepoint = c("0 h", "6 h","6 h", "0 h", "6 h", "6 h"),
                 treatment = c("baseline", "10NA", "NT","baseline", "10NA", "NT"),
                 mean_copy_no = c(1000, 1500, 1200, 600, 700, 400), 
                 sample_id = c(1, 2, 3, 4, 5, 6))

df %>%
  ggplot(aes(x=sample_id, y = mean_copy_no, fill = treatment)) +
  geom_col(colour = "black") +
  facet_nested(.~ pretreatment + timepoint + treatment, scales = "free", nest_line = TRUE, switch = "x") +
  ylim(0,2000) +
  theme_bw() +
  theme(strip.text.x = element_text(size = unit(10, "pt")),
        legend.position = "none",
        axis.title.y = element_markdown(size = unit(13, "pt")),
        axis.text.y = element_text(size = 11),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.text = element_markdown(size = unit(12, "pt")),
        strip.background = element_blank(),
        panel.spacing.x = unit(0.05,"line"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.border = element_blank())

This generates the following plot.
plot1

Now my problem is that each of the bars is located within its own facet and not all on one x axis (if you run the code without the theme, it shows it more clearly).

I have drawn how I would like the plot to look. desired_plot

I would like to add lines and stars to indicate significant differences.

I can easilly add the stars, however I am struggling to add the lines and I understand that this may not even be possible because I am using facets to generate the plot. I just wanted to post the question and see if someone has any suggestion on how to do it in R. Or if there is a way to achieve the nested look without using facets.

*Edited for clarity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤ぢ 2025-02-03 07:49:36

一个选项是在制作ggplot对象之后使用cowplot,我们可以在其中添加行和文本。

library(ggplot2)
library(cowplot)

results <- df %>%
  ggplot(aes(x=sample_id, y = mean_copy_no, fill = treatment)) +
  geom_col(colour = "black") +
  facet_nested(.~ pretreatment + timepoint + treatment, scales = "free", nest_line = TRUE, switch = "x") +
  ylim(0,2000) +
  theme_bw() +
  theme(strip.text.x = element_text(size = unit(10, "pt")),
        legend.position = "none",
        axis.title.y = element_markdown(size = unit(13, "pt")),
        axis.text.y = element_text(size = 11),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.text = element_markdown(size = unit(12, "pt")),
        strip.background = element_blank(),
        panel.spacing.x = unit(0.05,"line"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.border = element_blank())


ggdraw(results) +
  draw_line(
    x = c(0.07, 0.36),
    y = c(0.84, 0.84),
    color = "black", size = 1
  ) +
  annotate("text", x = 0.215, y = 0.85, label = "*", size = 15) +
  draw_line(
    x = c(0.7, 0.98),
    y = c(0.55, 0.55),
    color = "black", size = 1
  ) +
  annotate("text", x = 0.84, y = 0.56, label = "**", size = 15)

output

”在此处输入图像说明”

One option is to use cowplot after making the ggplot object, where we can add the lines and text.

library(ggplot2)
library(cowplot)

results <- df %>%
  ggplot(aes(x=sample_id, y = mean_copy_no, fill = treatment)) +
  geom_col(colour = "black") +
  facet_nested(.~ pretreatment + timepoint + treatment, scales = "free", nest_line = TRUE, switch = "x") +
  ylim(0,2000) +
  theme_bw() +
  theme(strip.text.x = element_text(size = unit(10, "pt")),
        legend.position = "none",
        axis.title.y = element_markdown(size = unit(13, "pt")),
        axis.text.y = element_text(size = 11),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.text = element_markdown(size = unit(12, "pt")),
        strip.background = element_blank(),
        panel.spacing.x = unit(0.05,"line"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.border = element_blank())


ggdraw(results) +
  draw_line(
    x = c(0.07, 0.36),
    y = c(0.84, 0.84),
    color = "black", size = 1
  ) +
  annotate("text", x = 0.215, y = 0.85, label = "*", size = 15) +
  draw_line(
    x = c(0.7, 0.98),
    y = c(0.55, 0.55),
    color = "black", size = 1
  ) +
  annotate("text", x = 0.84, y = 0.56, label = "**", size = 15)

Output

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文