添加多堆栈轴标签以进行绘图

时间:2018-04-07 04:49:14

标签: r ggplot2

我有一个名为“data”的数据集:

enter image description here

df=ddply(data,c("Treatment","Concentration"),summarise,mean=mean(Inhibition),sd=sd(Inhibition),n=length(Inhibition),se=sd/sqrt(n))

p <- ggplot(df, aes(x=Treatment, y=Inhibition))

p1 <- p + geom_bar(stat="identity", position="dodge") +
 geom_errorbar(aes(ymin=Inhibition-se,ymax=Inhibition+se), position="dodge",width=0.2)

我得到了以下图表:

enter image description here

我希望x轴如下图所示:

enter image description here

我是怎么做到的?

1 个答案:

答案 0 :(得分:3)

最好使用ggplot中的facet来实现。由于您没有包含可重用的数据集,我在这里做了一个:

df <- data.frame(Group = c("A", "A", "A", "A", "B"),
           SubGroup = c(letters[1:5]),
           value = 1:5
           )

请参阅下面的facet_grid行,其中指定了一些其他选项。您可以阅读有关添加的参数here

的更多信息
library(ggplot2)

ggplot(df, aes(x = SubGroup, value)) +
  geom_bar(stat="identity", position="dodge") + 
  facet_grid(.~Group, scales = "free_x", space = "free", switch = "x") +
  theme(strip.placement = "outside")

enter image description here

对于您的数据,您需要先将药物和剂量分成两个单独的列,就像我的例子一样。