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.
This section shows how to use the ggplot2 package to draw a plot based on two different data sets. For this, we have to set the data argument within the ggplot function to NULL. Then, we are specifying two geoms (i.e. geom_point and geom_line) and define the data set we want to use within each of those geoms.
The following code shows how to create the barplot with multiple variables using the geom_bar () function to create the bars and the ‘dodge’ argument to specify that the bars within each group should “dodge” each other and be displayed side by side. ggplot (df, aes(fill=food, y=sales, x=stadium)) + geom_bar (position='dodge', stat='identity')
Data derived from ToothGrowth data sets are used. ToothGrowth describes the effect of Vitamin C on Tooth growth in Guinea pigs. In the R code above, we used the argument stat = “identity” to make barplots. Note that, the default value of the argument stat is “bin”. In this case, the height of the bar represents the count of cases in each category.
It is also possible to change manually barplot fill colors using the functions : The allowed values for the arguments legend.position are : “left”,“top”, “right”, “bottom”. The function scale_x_discrete can be used to change the order of items to “2”, “0.5”, “1” : Data derived from ToothGrowth data sets are used.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With