gtwebb
gtwebb

Reputation: 3011

ggplot bar chart with two dataframes

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.

Upvotes: 7

Views: 14080

Answers (1)

Brian Diggs
Brian Diggs

Reputation: 58835

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).

enter image description here

Upvotes: 7

Related Questions