R 如何制作带有两个y变量并排和标准误差的条形图

R 如何制作带有两个y变量并排和标准误差的条形图,r,ggplot2,geom-bar,R,Ggplot2,Geom Bar,我有一个数据框,如下所示: df1<- data.frame(Hour=c(0,6,12,18,24,0,6,12,18,24), meanType=c("mean_A","mean_A","mean_A","mean_A","mean_A","mean_B","mean_B","mean_B","mean_B","mean_B"), mean=c(7.3,6.8,8.9,3.4,12.1,6.3,8.2,3.1,4.8,

我有一个数据框,如下所示:

df1<- data.frame(Hour=c(0,6,12,18,24,0,6,12,18,24),
                 meanType=c("mean_A","mean_A","mean_A","mean_A","mean_A","mean_B","mean_B","mean_B","mean_B","mean_B"),
                 mean=c(7.3,6.8,8.9,3.4,12.1,6.3,8.2,3.1,4.8,13.2),
                 Se=c(1.3,2.1,0.9,3.2,0.8,0.9,0.3,1.8,1.1,1.3))
df1

   Hour meanType mean  Se
1     0   mean_A  7.3 1.3
2     6   mean_A  6.8 2.1
3    12   mean_A  8.9 0.9
4    18   mean_A  3.4 3.2
5    24   mean_A 12.1 0.8
6     0   mean_B  6.3 0.9
7     6   mean_B  8.2 0.3
8    12   mean_B  3.1 1.8
9    18   mean_B  4.8 1.1
10   24   mean_B 13.2 1.3


df1您需要将
position\u dodge()
添加到
geom\u errorbar
中。此外,无需在每个
geom
中重复您的
aes
。可以使用
scale\u x
添加打断

库(ggplot2)
ggplot(df1,aes(x=小时,y=平均值,填充=平均值类型))+
几何图形栏(stat=“identity”,position=“dodge”)+
几何误差条(aes(ymin=平均Se,ymax=平均+Se),
宽度=0.2,颜色=“黑色”,阿尔法=0.9,尺寸=0.5,位置=position_道奇(5))+
连续缩放(间隔=df1$小时)

问题中的代码有两个问题

  • 对于要标记的所有值,
    x
    轴必须是离散的。这可以通过将
    小时数
    强制为类
    “因子”
    来解决
  • 错误条也必须具有
    位置\u dodge()
因此,代码的其余部分或多或少是相同的,只要它们相同,就会删除重复的
aes

Plot1 <- ggplot(df1, aes(factor(Hour), mean, fill = meanType)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_errorbar(aes(ymin = mean - Se, ymax = mean + Se),
                position = position_dodge(0.9),
                width = 0.4, colour = "orange", 
                alpha = 0.9, size = 0.5)

Plot1
Plot1
Plot1 <- ggplot(df1, aes(factor(Hour), mean, fill = meanType)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_errorbar(aes(ymin = mean - Se, ymax = mean + Se),
                position = position_dodge(0.9),
                width = 0.4, colour = "orange", 
                alpha = 0.9, size = 0.5)

Plot1