adjust bar width (not binwidth) in geom_histogram?

5,102 views
Skip to first unread message

Adam

unread,
Aug 22, 2011, 6:01:55 PM8/22/11
to ggplot2
Hello,

Is there a way to adjust the width of the bars when generating a
histogram using geom_histogram? In other words, I don't want adjacent
bars touching each other.

Thank you,
Adam

Ista Zahn

unread,
Aug 22, 2011, 6:21:54 PM8/22/11
to Adam, ggplot2
Hi Adam,

The bars in a histogram touch by definition. Why would you want to
confuse the issue by putting space between them?

Best.
Ista

>
> Thank you,
> Adam
>
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Mateusz Kaduk

unread,
Oct 14, 2015, 9:41:04 AM10/14/15
to ggplot2, adam...@gmail.com, iz...@psych.rochester.edu


On Tuesday, August 23, 2011 at 12:21:54 AM UTC+2, Ista Zahn wrote:

The bars in a histogram touch by definition. Why would you want to
confuse the issue by putting space between them?


I don't mean to be rude, but why would you spam mailing-list without answering the question ?!?! Please don't do that in future.

When it comes to question, ggplot does not support spacing between bars when independent variable is continuous, but does support that for discrete variable
You can enforce variable to be treated as discrete by using factor(..)
This might be useful if for some reason (maybe worth looking at in the first place) your variable is discrete but is treated as continuous.

For example to show histogram of hypothetical number of students in each class with bars separated
x = data.frame(class=c(1,1,1,1,1,1,2,2,3,3,3,4,4,4,4),id=1:15)
ggplot(x, aes(x=factor(class),y=..count..)) + geom_histogram()

Regards,
Mateusz




Reply all
Reply to author
Forward
0 new messages