Inconsistent histogram count

27 views
Skip to first unread message

sh k

unread,
Sep 30, 2020, 1:23:54 PM9/30/20
to ggplot2
g1<-  ggplot()+geom_histogram(bins=5,aes(x=1:5))+xlab("1 to 5")
g2<-  ggplot()+geom_histogram(bins=5,aes(x=1:5+0.5))+xlab("1.5 to 5.5")

g1 results in 1,1,1,1,1
g2 results in 2,1,1,1  #Missing one bin


Thanks in advance

Lorenzo Crippa

unread,
Sep 30, 2020, 1:29:09 PM9/30/20
to sh k, ggplot2
Hi,

You can use a geom_bar() instead of a geom_histogram to make sure you are counting the number of occurrences for each level in your x:

g2 <- ggplot() + geom_bar(aes(x = 1:5 + .5)) + xlab(“1.5 to 5.5”)

g2 will give you five bins, with one observation each.

Best,
Lorenzo

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/ab5114c0-73de-45b7-b2e4-28a5065c57f8n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages