Home > Software engineering >  how to make the histogram fill option work
how to make the histogram fill option work

Time:02-12

I want to see the distribution difference between two groups in my data. My data looks similar to

data <- data.frame(rnorm(500,mean = 3),sample(c(0,1), replace=TRUE, size=500))
colnames(data) <- c('x','y')

And then I draw the histogram by

ggplot(data, aes(x = x, fill = y))  
   geom_histogram(alpha=0.6, bins = 50, position = 'identity')

But it returns

enter image description here

Can you please help find what the problem is? I just followed the examples from googling. Thank you!

CodePudding user response:

Assuming you want y as discrete groups, you should convert it to a factor

ggplot(data, aes(x = x, fill = factor(y)))  
  geom_histogram(alpha=0.6, bins = 50, position = 'identity')

enter image description here

  • Related