Reputation: 21
I have a question about the binwidth in ggplot in r.
I have two sets of data, one called "error_hat" and one called "error_tilde". I have made their histogram separately, and I see they are similar to each other.
Now I want to put them together to make a comparison. My code is as followed:
catagory <- c(rep("error_hat",length(error_hat)) , rep("error_tilde",length(error_tilde)))
error <- c(error_hat, error_tilde)
error_data<-data.frame(catagory,error)
ggplot(error_data, aes(x=error,group=catagory,fill=catagory))+
geom_histogram(position="dodge2", binwidth=0.03)+theme_bw()
It produces a picture like this:
I am wondering why the data in the middle has a different width (since I have set all the width to be 0.03)?
Could anyone help me with this problem? Many thanks!
Upvotes: 2
Views: 2376
Reputation: 69221
This is a consequence of using dodge2
versus dodge
. This is the expected behavior as outlined here.
Maybe you want regular dodge
instead?
library(ggplot2)
#fake data that mimics yours
set.seed(42)
error_hat <- runif(100)
error_tilde <- runif(100)
catagory <- c(rep("error_hat",length(error_hat)) , rep("error_tilde",length(error_tilde)))
error <- c(error_hat, error_tilde)
error_data<-data.frame(catagory,error)
ggplot(error_data, aes(x=error,group=catagory,fill=catagory))+
geom_histogram(position="dodge", binwidth=0.03)+theme_bw()
Created on 2019-01-15 by the reprex package (v0.2.1)
Upvotes: 1