Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

ggplot2 - R Plot Bars are disappearing in grid.arrange/plot_grid

I have a problem with bars from my plots disappearing when I combine multiple plots.

Here is the code I′m using:

station<-c("Eins", "Zwei", "Drei","Vier")
x1<-c(200, 185, 165,180)
x2<-c(185,150,148,25)
x3<-c(156,125,141,36)

test.data<-data.frame(station,x1,x2,x3)


plot_sof<-ggplot(test.data, aes(x=station, y=x1))
plot_sof + geom_bar(stat="identity",na.rm=TRUE)

plot_sof_1<-ggplot(test.data, aes(x=station, y=x2))
plot_sof_1+geom_bar(stat = "identity",na.rm=TRUE)

plot_sof_2<-ggplot(test.data, aes(x=station, y=x3))
plot_sof_2+geom_bar(stat = "identity",na.rm=TRUE)

plot_grid(plot_sof, plot_sof_1, plot_sof_2, labels=c("x1", "x2", "x3"))

grid.arrange(plot_sof, plot_sof_1, plot_sof_2, ncol=1)```

Example of one of the plots which should combined

First try to combine the plots via grid.arrange, the bars are missing

Second try via plot_grid, here are the bars also missing

Does anyone know why the bars are missing and maybe know a solution or a workaround for that problem?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try with this, you have to assign the new geom to your objects:

#Code
plot_sof<-ggplot(test.data, aes(x=station, y=x1))
plot_sof <-plot_sof + geom_bar(stat="identity",na.rm=TRUE)

plot_sof_1<-ggplot(test.data, aes(x=station, y=x2))
plot_sof_1 <- plot_sof_1+geom_bar(stat = "identity",na.rm=TRUE)

plot_sof_2<-ggplot(test.data, aes(x=station, y=x3))
plot_sof_2 <- plot_sof_2+geom_bar(stat = "identity",na.rm=TRUE)

plot_grid(plot_sof, plot_sof_1, plot_sof_2, labels=c("x1", "x2", "x3"))

grid.arrange(plot_sof, plot_sof_1, plot_sof_2, ncol=1)

Outputs:

enter image description here

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...