在多个方面注释GGPLOT2
最近,我开始使用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.
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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个选项是在制作
ggplot
对象之后使用cowplot
,我们可以在其中添加行和文本。output
One option is to use
cowplot
after making theggplot
object, where we can add the lines and text.Output