turn off bin outlines in histogram?

943 views
Skip to first unread message

David Morrison

unread,
Aug 2, 2011, 12:20:33 AM8/2/11
to ggp...@googlegroups.com
This code produces a pretty typical histogram plot

ggplot(data.frame(x=rnorm(1000,10,1)))+opts(aspect.ratio=0.7)+geom_histogram(aes(x=x),binwidth=0.2,fill="white",color="black")+theme_bw()

in which each edge of each bin is inked.  In my field it's much more common to remove the vertical lines separating adjacent bins - as in the plot below.  Is it possible to  to do this in ggplot2?  I poked around in the documentation and scrolled though galleries of R plots, but I honestly couldn't find any way to do this.

- Dave


ONKELINX, Thierry

unread,
Aug 2, 2011, 12:48:30 AM8/2/11
to ggp...@googlegroups.com
Dave,

I don't think that it's possible. There are two possible work-arounds.

1) set fill to something other than white and colour to NULL
2) use geom_density instead of geom_histogram

Best regards,

Thierry

Van: ggp...@googlegroups.com [ggp...@googlegroups.com] namens David Morrison [da...@bnl.gov]
Verzonden: dinsdag 2 augustus 2011 6:20
Aan: ggp...@googlegroups.com
Onderwerp: turn off bin outlines in histogram?

--
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

David Morrison

unread,
Aug 3, 2011, 5:15:43 PM8/3/11
to ggp...@googlegroups.com
How's this for a solution?

x<-rnorm(1000)
h<-hist(x,30)

v<-vector(mode="numeric")
for (i in h$counts) {
 v<-c(v,i,i)
}
v<-c(0,v,0)
u<-vector(mode="numeric")
for (i in h$breaks) {
 u<-c(u,i,i)
}

d<-data.frame(u,v)

ggplot(d)+geom_polygon(aes(x=u,y=v),fill="white",color="black")+geom_hline(y=0)+theme_bw()

It's not very convenient, but I suppose it works.

- Dave

baptiste auguie

unread,
Aug 3, 2011, 5:19:50 PM8/3/11
to ggp...@googlegroups.com
geom_step instead?

ggplot(data.frame(x=rnorm(1000,10,1)))+opts(aspect.ratio=0.7) + geom_step(aes(x=x),stat="bin",binwidth=0.2,fill="white",color="black")+theme_bw()

HTH,

baptiste

David Morrison

unread,
Aug 3, 2011, 6:26:13 PM8/3/11
to ggp...@googlegroups.com
That's very nice.  Much more straightforward than what I came up with.  The only downside - and this isn't a big deal from my perspective - is that the geom_step doesn't have an "inside", so I don't think the fill has any effect.   At least, it didn't when I tried changing it.  But very nice.
Reply all
Reply to author
Forward
0 new messages