Histogram with a non zero origin

1,431 views
Skip to first unread message

Pierre B. de Villemereuil

unread,
Nov 21, 2011, 11:06:52 AM11/21/11
to ggplot2
Dear all,


I'm trying to plot a histogram of values around 95%. For easier reading,
I'd like to start histogram at 60% instead of zero. Unfortunately, I
couldn't find a way to do it !

Here is a reproducible example:
y<-runif(16,0.6,1)
x<-c(rep("A",4),rep("B",4),rep("C",4),rep("D",4))
type<-rep(c("1","2","3","4"),4)
data<-data.frame(x=x,y=y,type=type)

Here is a plot starting from zero :
ggplot(data,aes(x=x,y=y))+geom_bar(aes(fill=type),colour='black',position="dodge",stat="identity")+scale_y_continuous(formatter="percent")

However, when "limiting" y values, the plot just disappears:
ggplot(data,aes(x=x,y=y))+geom_bar(aes(fill=type),colour='black',position="dodge",stat="identity")+scale_y_continuous(limits=c(0.6,1),formatter="percent")

I also tried to use the (not documented) "origin" argument of stat_bin()
function, but it is exactly identical to the first plot (seems like
"origin" does nothing):
ggplot(data,aes(x=x,y=y))+stat_bin(aes(fill=type),colour='black',position="dodge",origin=0.6,geom="histogram")+scale_y_continuous(formatter="percent")

Does anyone has an idea how to start a histogram from an arbitrary value
instead of 0 ?

Thanks in advance for any help !

Cheers,
Pierre.


Winston Chang

unread,
Nov 21, 2011, 12:37:30 PM11/21/11
to bon...@magbio.ens.fr, ggplot2
Pierre -

You can use coord_cartesian to set the viewing area of the graph. When you use scale_y_continuous, it also clips the data to fit into the range -- any bars that extend out of the range will not show.

This should do the trick:
ggplot(data,aes(x=x,y=y)) +
    geom_bar(aes(fill=type),colour='black',position="dodge",stat="identity") +
    coord_cartesian(ylim=c(.6,1))

Using coord_cartesian doesn't change the placement of the ticks, however, so you may need to specify the breaks in scale_y_continuous manually, with something like this:
ggplot(data,aes(x=x,y=y)) +
    geom_bar(aes(fill=type),colour='black',position="dodge",stat="identity") +
    coord_cartesian(ylim=c(.6,1)) +
    scale_y_continuous(breaks=seq(0,1,.1))


In the development version of ggplot, there's no need to use coord_cartesian; you can have bars go out of the viewable range by setting scale_y_continuous.


Also, "origin" sets the starting point along the x axis (not y) for geom_histogram.

Hope this helps,
-Winston




--
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+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Hadley Wickham

unread,
Nov 21, 2011, 12:40:23 PM11/21/11
to Winston Chang, bon...@magbio.ens.fr, ggplot2
> In the development version of ggplot, there's no need to use
> coord_cartesian; you can have bars go out of the viewable range by setting
> scale_y_continuous.

That might be a bug...

Hadley


--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Winston Chang

unread,
Nov 21, 2011, 12:57:47 PM11/21/11
to Hadley Wickham, bon...@magbio.ens.fr, ggplot2
Really? I've actually found it very useful to have things work this way...

Hadley Wickham

unread,
Nov 21, 2011, 12:59:58 PM11/21/11
to Winston Chang, bon...@magbio.ens.fr, ggplot2
Yes, I'm not sure yet if I want to change the behaviour or not. It
certainly needs to be done with some thought (especially the
implications for setting limits on legends)

Hadley

Reply all
Reply to author
Forward
0 new messages