Error while plotting overlaid histograms in R using ggplot2

r
kaggle
datavisualization
ggplot2

#1

Hi,

While using ggplot2 to plot overalaid histograms on a dataset (bike sharing demand from kaggle) ->

ggplot(train, aes(x=temp, fill=day)) +

  • geom_histogram(binwidth=.5, alpha=.5, position="identity")
    

I am getting an error->

Error in unit(tic_pos.c, “mm”) : ‘x’ and ‘units’ must have length > 0

Why is it coming and how to remove it?

data frame->

Thanks.


#2

Hi Aditya,

You got this error because day values are numeric in your data frame. In this case for the fill= you need discrete values as bars are stacked and will be colored according to days. Just use as.factor() around days.

ggplot(train, aes(x=temp, fill=as.factor(day))) +

  • geom_histogram(binwidth=.5, alpha=.5, position=“identity”)

Hope it helps!