如何制作带有两个并排的 y 变量和标准误差的条形图
How to make a barplot with two y-variables side by side and standar errors
我有一个如下所示的数据框:
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
我想创建一个条形图,其中 X 轴上的小时代表数据框中出现的实际小时,我还想在每个条上添加一个误差条,指示与每个均值相关的误差。
这是我目前得到的:
Plot1<-ggplot(df1,aes(Hour,mean,fill=meanType))+
geom_bar(aes(x=Hour, y=mean, fill=meanType),stat="identity",position="dodge")+
geom_errorbar(aes(x=Hour,y=mean,ymin=mean-Se, ymax=mean+Se), width=0.4, colour="orange", alpha=0.9, size=0.5)
Plot1
但是,我不知道为什么,但错误栏没有很好地调整,并且在 X 轴上,小时数似乎是 "random"(不是我的数据框中真正的小时数)。
有谁知道如何解决这些问题?
您需要将 position_dodge()
添加到您的 geom_errorbar
。此外,无需在每个 geom
中重复您的 aes
。可以使用 scale_x
.
添加中断
library(ggplot2)
ggplot(df1,aes(x=Hour,y=mean,fill=meanType))+
geom_bar(stat="identity",position="dodge")+
geom_errorbar(aes(ymin=mean-Se,ymax=mean+Se),
width=0.2,colour="black",alpha=0.9,size=0.5,position=position_dodge(5)) +
scale_x_continuous(breaks = df1$Hour)
题中代码有两个问题
- 对于要标记的所有值,
x
轴必须是离散的。这通过强制 Hour
到 class "factor"
. 来解决
- 误差线也必须有
position_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
我有一个如下所示的数据框:
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
我想创建一个条形图,其中 X 轴上的小时代表数据框中出现的实际小时,我还想在每个条上添加一个误差条,指示与每个均值相关的误差。
这是我目前得到的:
Plot1<-ggplot(df1,aes(Hour,mean,fill=meanType))+
geom_bar(aes(x=Hour, y=mean, fill=meanType),stat="identity",position="dodge")+
geom_errorbar(aes(x=Hour,y=mean,ymin=mean-Se, ymax=mean+Se), width=0.4, colour="orange", alpha=0.9, size=0.5)
Plot1
有谁知道如何解决这些问题?
您需要将 position_dodge()
添加到您的 geom_errorbar
。此外,无需在每个 geom
中重复您的 aes
。可以使用 scale_x
.
library(ggplot2)
ggplot(df1,aes(x=Hour,y=mean,fill=meanType))+
geom_bar(stat="identity",position="dodge")+
geom_errorbar(aes(ymin=mean-Se,ymax=mean+Se),
width=0.2,colour="black",alpha=0.9,size=0.5,position=position_dodge(5)) +
scale_x_continuous(breaks = df1$Hour)
题中代码有两个问题
- 对于要标记的所有值,
x
轴必须是离散的。这通过强制Hour
到 class"factor"
. 来解决
- 误差线也必须有
position_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