Histogram respecting scale x limits?

2,856 views
Skip to first unread message

Allan Engelhardt

unread,
Mar 9, 2012, 8:41:00 AM3/9/12
to ggp...@googlegroups.com
This is possibly a dumb question, but if I do

library("ggplot")
X <- data.frame(x = rep(1:10, length.out=13))
ggplot(X, aes(x)) + geom_histogram() + scale_x_discrete(limits = c(1, 5))


I still get a plot on 1:10 on the abscissa (x axis), though the labels are limited (to the values 1 and 5).  I wanted to limit the range of the abscissa to [1; 5] - what is the correct incantation for that (other than removing the data from X, which is my workaround)?

Allan

> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C            
 [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8   
 [5] LC_MONETARY=en_GB.utf8    LC_MESSAGES=en_GB.utf8  
 [7] LC_PAPER=C                LC_NAME=C               
 [9] LC_ADDRESS=C              LC_TELEPHONE=C          
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C     

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods 
[8] base    

other attached packages:
[1] ggplot2_0.9.0     data.table_1.7.10 ctv_0.7-4       

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   compiler_2.14.1    dichromat_1.2-4    digest_0.5.1     
 [5] MASS_7.3-17        memoise_0.1        munsell_0.3        plyr_1.7.1       
 [9] proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1     scales_0.2.0     
[13] stringr_0.6        tools_2.14.1     
>

Ben Bond-Lamberty

unread,
Mar 9, 2012, 9:00:04 AM3/9/12
to ggplot2

You probably want to use +coord_cartesian(xlim=c(1,5)). There are good examples of this on the ggplot web page.
Ben

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

Allan Engelhardt

unread,
Mar 9, 2012, 12:33:12 PM3/9/12
to Ben Bond-Lamberty, ggplot2
On 09/03/12 14:00, Ben Bond-Lamberty wrote:

You probably want to use +coord_cartesian(xlim=c(1,5)).


With xlim = c(0.5, 5.5) it more or less does what I want.  But I still find it confusing that these two (adapted from help("geom_histogram") work so very differently:


library("ggplot2")  # I meant ggplot2 below as well, of course...
diamonds_small <- diamonds[sample(nrow(diamonds), 1000),
ggplot(diamonds_small, aes(depth)) + geom_histogram() + scale_x_continuous(limits = c(55, 70))
ggplot(diamonds_small, aes(depth)) + geom_histogram() + scale_x_discrete(limits = c(55, 70))


In the second case, the limits only apply to the labels, not to the data displayed or the size of the x axis.  Adding +xlim(55, 70) in the second instance brings the two examples into line again.  My head hurts :)

Allan

Hadley Wickham

unread,
Apr 11, 2012, 3:22:56 PM4/11/12
to Allan Engelhardt, Ben Bond-Lamberty, ggplot2
> With xlim = c(0.5, 5.5) it more or less does what I want.  But I still find
> it confusing that these two (adapted from help("geom_histogram") work so
> very differently:
>
>
> library("ggplot2")  # I meant ggplot2 below as well, of course...
> diamonds_small <- diamonds[sample(nrow(diamonds), 1000),
> ggplot(diamonds_small, aes(depth)) + geom_histogram() +
> scale_x_continuous(limits = c(55, 70))
> ggplot(diamonds_small, aes(depth)) + geom_histogram() +
> scale_x_discrete(limits = c(55, 70))
>
>
> In the second case, the limits only apply to the labels, not to the data
> displayed or the size of the x axis.

Hmmm, this is not exactly a bug but it's a rather subtle feature of
discrete scales. The basic problem is that discrete scales also have
a continuous component so that (e.g.) jittering can work. That's what
you're seeing here, since you're really supplying continuous data to a
discrete scale.

>   Adding +xlim(55, 70) in the second
> instance brings the two examples into line again.  My head hurts :)

Because that's just a shortcut for scale_x_continuous(limits = c(55, 70))

Hadley

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

Reply all
Reply to author
Forward
0 new messages