ggplot bar chart with two dataframes

匿名 (未验证) 提交于 2019-12-03 08:42:37

问题:

What I am trying to do is create a "dodged" bar chart using gglot from two different dataframes

Unfortunately geom_bar doesn't see the previous data added so it plots it right over top, I've tried playing with position and width but it doesn't seem to change anything probably due to the fact that it is one bar per category.

The code below creates the data plots the data incorrectly (bars are on top of one another) and then plots it correctly using a workaround of binding the dataframes together.

library("ggplot2")  x<-data.frame(dat=rep(seq(1,4),3),let=rep("X")) y<-data.frame(dat=rep(seq(1,4),4),let=rep("y"))  xy<-rbind(x,y)  #what I would like to use with two different data frames ggplot(NULL,aes(dat))+   geom_bar(data=y,fill="red",width=0.1,position = "dodge")+   geom_bar(data=x,fill="blue",width=0.1,position = "dodge")  #what I would like to see only without binding dfs ggplot(xy,aes(dat,fill=let))+geom_bar(position="dodge") 

I'm using ggplot to be constant with other plots that use only a single dataframe.

回答1:

ggplot(mapping=aes(x=dat))+   geom_bar(data=y, aes(x=dat-0.1), fill="red", binwidth=0.1)+   geom_bar(data=x, fill="blue", binwidth=0.1) 

The key here is that you are shifting the data by the same amount as one binwidth and that binwidth is less than the spacing between groups. The binning is done on the data after shifting, so that affects which bin the data appears in. Also, without setting the binwidth explicitly, how wide the bins are depend on the range of the plot (which is why it varies when xlim was varied and worked "nicely" for round values).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!