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
283 views
in Technique[技术] by (71.8m points)

r - How to have geom_point and geom_bar side by side and with facet_wrap

I am trying to combine a scatterplot with a bar plot. The bar plot should be on the right side of the scatterplot of each corresponding group specified in facet wrap. The two plots have the same unit on the y axis but the scaling of the bar might need to be adjusted so the barplot it's not too tall.

Here's an example.

data(mpg)

So far I have tried this, but I would the bar should be outside the scatterplot area. Additionally, in my data the x axis in the barplot is a factor with two levels, so it has a discrete scale and my scatterplot has a continuous x scale.

ggplot()+geom_bar(data=mpg,aes(x=10,y=displ,fill=trans),stat='identity',position='stack')
+geom_point(data=mpg,aes(x=displ,y=displ))+facet_wrap(~cyl)

enter image description here

Can I achieve to plot these two plot types side-by-side or do I have to use something like cowplot? I think that if I have to combine plots outside ggplot I will not be able to use factor_wrap, so I guess I have to do a for loop for each factor level and then combine everything?

question from:https://stackoverflow.com/questions/65845330/how-to-have-geom-point-and-geom-bar-side-by-side-and-with-facet-wrap

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...