Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to make the histogram fill option work

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading