如何在ggplot中分组和标记geom_col?

我正在尝试使用R和ggplot2从GraphPad复制图形。 我有几种情况下的少量样品值:

df <- data.frame(Sample = c("blank","blank","A","B","C","C"),Condition = c("control","control","C1","C2","C2"),Value = c(0.719,1.25,0.687,8.19,4.68,3.53,14.1,7.11,8.8,6.48,7.05,4.82,6.32,4.97,6.97,5.5,7.22,6.89,8.89,6.83,8.73))

df$Sample <- factor(df$Sample,levels = c("blank","C")) 
df$Condition <- factor(df$Condition,levels = c("control","C2"))

我正在努力将它们按组(按条件)和标记(按样本)组织和绘制,如下所示:

如何在ggplot中分组和标记geom_col?

我尝试使用fillposition="dodge",但这完全不是我想要的:

df %>%
  ggplot + 
  aes(x = Sample,y = Value,fill = Condition) + 
  geom_col(position = "dodge")

如何在ggplot中分组和标记geom_col?

请注意,我也想从图例中排除空白。

我正在尝试创建,绘制和标记另一列,例如:

df <- df %>%
  mutate(Sample.Condition = paste(Sample,Condition,sep = "."))

..但是它变得太复杂了。为了学习,我正在寻找简单,整洁的解决方案。 谢谢!

vbsnowy 回答:如何在ggplot中分组和标记geom_col?

最有效的解决方案可能是引入刻面。

df %>%
  ggplot + 
  aes(x = Sample,y = Value,fill = Condition) + 
  geom_col() +
  facet_wrap(~Condition)
,

我不知道您是否成功获得了自己的情节,如果您对此感兴趣,我找到了一种解决方法:

首先,我为您的数据集(这是在Graphpad上表示的内容)计算mean的{​​{1}}和sd,然后按照@Gregor的建议,创建一个新列是ValuesSample

的串联
Condition

然后,我们可以绘制数据:

library(dplyr)
df2 = df%>%
  group_by(Sample,Condition) %>%
  summarise(Mean = mean(Value),Sd = sd(Value)) %>%
  mutate(New_Var = paste0(Sample,Condition))

该图看起来与从GraphPad获得的图非常相似。我同意这不是一件容易的事,但是如果您真的想要这个情节,您可以得到它。 enter image description here

编辑-在图形上添加单个值

library(ggplot2)
ggplot(df2,aes(x = New_Var,y = Mean,fill = Condition)) +
  geom_bar(stat = "identity",color = "black",position = position_dodge(),width = 0.7) +
  geom_errorbar(aes(ymin = Mean - Sd,ymax = Mean + Sd),width = .2,position = position_dodge(.9)) +
  scale_fill_manual(values = c("black","grey","red"),labels = c("Control","Condition 1","Condition 2")) +
  scale_x_discrete(limits = c("blankcontrol","AC1","BC1","CC1","AC2","BC2","CC2"),labels = c("Blank","A","B","C","C")) +
  theme(axis.text.x = element_text(face = "bold",angle = 45),legend.title = element_blank()) +
  xlab("") +
  scale_y_continuous(limits = c(0,15),breaks = c(0,5,10,15))
library(dplyr)
dfX= df %>%
  mutate(New_Var2 = paste0(Sample,Condition))

您将获得以下图形: enter image description here

但是,每个条件下只有3个点,我宁愿将均值表示为一个点,而sd则作为误差线。这样的事情。

library(ggplot2)
ggplot(df2,15))+
  geom_jitter(data = dfX,aes(x = New_Var2,y = Value),position=position_jitter(0.3),show.legend = F)

和结果图: enter image description here

但这只是我个人的意见,由您决定;)

本文链接:https://www.f2er.com/3105287.html

大家都在问