y-axis too big after xlim

96 views
Skip to first unread message

Hermann Norpois

unread,
Jan 15, 2013, 3:39:16 AM1/15/13
to ggp...@googlegroups.com
Hello,

if I focus on a defined interval to plot my data (e.g. scale_x_continuous or xlim) I still have a y-axis that comprises the range of the complete dataset. I would like the y-axis to be restricted in respect to my choice of xlim or scale_x_continuous).

My example:

> dput (dft)
structure(list(n = 1:10, m = c(2, 4, 6, 8, 10, 12, 100, 20, 18,
16)), .Names = c("n", "m"), row.names = c(NA, -10L), class = "data.frame")
> dft
    n   m
1   1   2
2   2   4
3   3   6
4   4   8
5   5  10
6   6  12
7   7 100
8   8  20
9   9  18
10 10  16


> ggplot (dft)+ geom_point (aes (x=n, y=m))+ xlim (c (1,5))   # the y-axis comprises 100 though 10 would be enough

> ggplot (dft)+ geom_point (aes (x=n, y=m))+ scale_x_continuous (limits = c(1,4)) # the y-axis comprises 100 though 8 would be ...


I want the y-axis to be restricted on the y-data that appears in my choice of the x-interval.
How does it work?

Thanks
Hermann

Dennis Murphy

unread,
Jan 15, 2013, 3:45:15 AM1/15/13
to Hermann Norpois, ggp...@googlegroups.com
scale_*_continuous() and xlim()/ylim() only restrict axes in one
dimension. If you subset your data, you get exactly what you want:

ggplot(subset(dft, n <= 5)) + geom_point(aes(x = n, y = m))
ggplot(subset(dft, n <= 4)) + geom_point(aes(x = n, y = m))

Dennis

Derek Borba

unread,
Jan 15, 2013, 6:47:16 AM1/15/13
to Hermann Norpois, ggplot2
Here's one solution to your problem, Hermann. 

Use of coord_cartesian() for zooming:
http://docs.ggplot2.org/0.9.3/coord_cartesian.html

########################
####   Your examples   ####
########################
#  Remove all objects from workspace
rm(list = ls())

#  Load ggplot2 library
library(ggplot2)

#  Prepare data.frame
dft <- structure(list(n = 1:10,
                      m = c(2, 4, 6, 8, 10, 12, 100, 20, 18, 16)),
                      .Names = c("n", "m"),
                      row.names = c(NA, -10L),
                      class = "data.frame")

#  Confirm data.frame structure
str(dft)

#  First plot example with xlim()
ggplot (dft) +
  geom_point (aes (x=n, y=m)) +
  xlim (c (1,5))

#  Second plot example with scale_x_continuous()
ggplot (dft) +
  geom_point (aes (x=n, y=m)) +
  scale_x_continuous (limits = c(1,4))

##########################
######  end examples  ######
##########################

##########################
####  Possible solutions  ####
##########################
#  Using coord_cartesian() for zooming
#  x-axis restricted to 0 and 5 (xlim)
#  y-axis restricted to 0 and 10 (ylim)
ggplot (dft) +
  geom_point (aes (x=n, y=m)) +
  coord_cartesian(xlim = c(0, 5), ylim = c(0,10)) +
  ggtitle("Using coord_cartesian as a zooming function\nExample 1")


#  Using coord_cartesian() for zooming
#  x-axis restricted to 0 and 4 (xlim)
#  y-axis restricted to 0 and 8 (ylim)
ggplot (dft) +
  geom_point(aes (x = n, y = m)) +
  coord_cartesian(xlim = c(0, 4), ylim = c(0, 8)) +
  ggtitle('Using coord_cartesian as a zooming function\nExample 2')


#  Alternatively, you could also place the data object and primary aesthetics inside of ggplot() and
#  fine tune your plot with breaks, labels and a title like so...
ggplot (dft, aes(n, m)) +
  theme_bw() +
  geom_point() +
  coord_cartesian(xlim = c(0, 5.25), ylim = c(0, 10.25)) +
  scale_y_continuous(breaks = seq(0,12, by = 1.0)) +
  labs(x = 'n', y = 'm') +
  ggtitle('Using coord_cartesian as a zooming function\nExample 3')

##########################
##### end pos_solutions  ####
##########################

Hope this helps.

Derek


On Tue, Jan 15, 2013 at 12:39 AM, Hermann Norpois <hnor...@googlemail.com> wrote:

Hermann

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

Hermann Norpois

unread,
Jan 15, 2013, 11:47:41 AM1/15/13
to Derek Borba, ggplot2
Thank you very much. I guess this  helps.



@ DennisMurphy
This was what I did so far. But I did not like it and was looking for an alternative.

Thanks
Hermann
2013/1/15 Derek Borba <derek...@gmail.com>
Reply all
Reply to author
Forward
0 new messages