我正在使用以下数据集,并尝试使用ggplot生成条形图。之前,我曾以相同的格式使用过多次,但是无论我如何重新格式化数据,在这种情况下都无法获得gplot代码来正确识别因素“年”。任何帮助,将不胜感激。谢谢。
Year<-c(2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017)
Strat<-c(0,0,5,5,10,10,15,15,20,20,0,0,5,5,10,10,15,15,20,20)
Scar<-c(1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2)
GDMean<-c(4,3,4,3,4,3,5,5,4,4,4,3,3,2,3,3,3,2,3,2)
GDSE<-c(0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0)
GPMean<-c(87,91,88,99,91,92,78,92,80,86,72,85,90,83,92,89,86,88,82,88)
GPSE<-c(2,2,3,1,3,2,3,2,2,2,6,4,3,4,2,3,3,2,5,4)
YZ<-cbind(Year,Strat,Scar,GDMean,GDSE,GPMean,GPSE)
YZ<-data.frame(YZ)
YZ$Strat<-as.factor(YZ$Strat)
YZ$Year<-as.factor(YZ$Year)
YZ$Scar<-as.factor(YZ$Scar)
YZ<-data.frame(YZ)
GRAPH<- ggplot(data=YZ,aes(x=YZ$Year, y=YZ$GPMean, fill=YZ$Scar))+geom_bargeom_bar(position='dodge',stat='identity',width=0.7)+
facet_grid(. ~ YZ$Strat)+
geom_errorbar(aes(ymin=YZ$GPMean-YZ$GPSE, ymax=YZ$GPMean+YZ$GPSE), position = position_dodge(), width = 0.7)+
theme_bw()+
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank(),axis.line=elment_line(colour="black"),panel.border=element_blank(),legend.position="none")+
scale_fill_grey()+
ylab("Max")
GRAPH
然后我得到这样的东西:
答案 0 :(得分:1)
不建议在$
调用中使用ggplot()
来引用数据帧的列
GRAPH <- ggplot(data = YZ, aes(x = Year, y = GPMean, fill = Scar)) +
geom_bar(position = "dodge", stat = "identity", width = 0.7) +
facet_grid(. ~ Strat) +
geom_errorbar(aes(ymin = GPMean - GPSE, ymax = GPMean + GPSE),
position = position_dodge(), width = 0.7) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_blank(),
legend.position = "none") +
scale_fill_grey() +
ylab("Max")
GRAPH
由reprex package(v0.2.1.9000)于2018-09-21创建