missing data when axis limits set

95 views
Skip to first unread message

squid

unread,
Aug 14, 2009, 6:39:13 AM8/14/09
to ggplot2
Hi,
I have data that ranges from 0 to 0.02 on the y axis and I want to
zoom in on a few plots so that only the data from 0 to 0.01 is viewed.
I do this using ylim(0, 0.01) but when I use this, the geom_line
plotted values that fall above the ylimit disappear entirely rather
than just hitting the top of the plot. Is there a way to avoid losing
these extreme values?
Thank you,
squid.

LSelzer

unread,
Aug 14, 2009, 8:54:06 AM8/14/09
to ggplot2
Hey,
you can do that with scale_y_continous(limits=c(0,0.01))
Here's an example from the book to make it clearer:

p <- qplot(disp, wt, data=mtcars) + geom_smooth()
p + scale_x_continuous(limits = c(325, 500))

Luciano

dpark

unread,
Sep 28, 2009, 10:47:55 AM9/28/09
to ggplot2
Hi,

This doesn't address the issue.. for me, this is my main complaint
about ggplot, and I'm not sure how to get around it. It's an issue
with geom_area and geom_line (and presumably _freqpoly), but not R's
native plotting engines.

Here is an example scenario:

t$x <- seq(100)
t$y <- dnorm(t$x, mean=50, sd=10)
ggplot(t) + geom_area(aes(x, y))

That looks like a perfectly normal bell curve. Now add either ylim
(0,0.02) or scale_y_continuous(limits=c(0,0.02)) into the mix and look
at what you get. Two spikes with emptiness in the middle.

It's really quite misleading on a number of levels. Depending on what
the data looks like, your eye may be lead to believe that it's
plotting zero values instead of "off the chart" values. Even it's
clear that it is not plotting zeroes, it is at the very least
suggesting that you have missing data in those regions... but my input
did not contain any NAs.. there really are plottable values there.

Omitting the out-of-range values (and complaining that you "missed
some data points due to small ylim range") is okay for geom_point, but
not for geom_area or geom_line (especially geom_area).

I suppose the data could be fed through some home-made range-limiting
function before passing it to ggplot, but even that would be somewhat
misleading for, say, situations where we had much less data points.
The slope of the line going off the page would potentially not be
steep enough in some situations.

I am using ggplot2_0.8.2 on R 2.8.1 GUI 1.27 Tiger build 32-bit
(5301). Perhaps this isn't an issue in other versions or platforms?

Thanks,

smu

unread,
Sep 28, 2009, 11:00:39 AM9/28/09
to dpark, ggplot2
hi,

On Mon, Sep 28, 2009 at 07:47:55AM -0700, dpark wrote:
>
> Here is an example scenario:
>
> t$x <- seq(100)
> t$y <- dnorm(t$x, mean=50, sd=10)
> ggplot(t) + geom_area(aes(x, y))
>
> That looks like a perfectly normal bell curve. Now add either ylim
> (0,0.02) or scale_y_continuous(limits=c(0,0.02)) into the mix and look
> at what you get. Two spikes with emptiness in the middle.
>

not for me, see the attached file.

regards,
Stefan

example.png

smu

unread,
Sep 28, 2009, 11:02:55 AM9/28/09
to ggplot2

I forgot:

> sessionInfo()
R version 2.9.1 (2009-06-26)
i686-pc-linux-gnu

locale:
LC_CTYPE=de_DE.UTF-8;LC_NUMERIC=C;LC_TIME=de_DE.UTF-8;LC_COLLATE=de_DE.UTF-8;LC_MONETARY=C;LC_MESSAGES=de_DE.UTF-8;LC_PAPER=de_DE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=de_DE.UTF-8;LC_IDENTIFICATION=C

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

other attached packages:
[1] ggplot2_0.8.3 reshape_0.8.3 plyr_0.1.9 proto_0.3-8

loaded via a namespace (and not attached):
[1] tools_2.9.1

hadley wickham

unread,
Sep 28, 2009, 11:03:58 AM9/28/09
to dpark, ggplot2
> It's really quite misleading on a number of levels.  Depending on what
> the data looks like, your eye may be lead to believe that it's
> plotting zero values instead of "off the chart" values.  Even it's
> clear that it is not plotting zeroes, it is at the very least
> suggesting that you have missing data in those regions... but my input
> did not contain any NAs.. there really are plottable values there.

No, there aren't - because you've restricted the scale to exclude
them. You probably want the xlim and ylim arguments to
coord_cartesian to zoom into the region of interest.

Hadley


--
http://had.co.nz/

dpark

unread,
Sep 28, 2009, 11:47:11 AM9/28/09
to ggplot2
Interesting... thank you everyone for all the feedback. Both
solutions worked: using coord_cartesian (instead of ylim/
scale_y_continuous) plots geom_area and geom_line the way I would
expect. And upgrading my R and ggplot to the latest versions make
ylim/scale_y_continuous produce the same output as Stefan... though
interestingly, the coord_cartesian plot is cleaner, because it
actually shows data going off the chart, whereas the ylim/
scale_y_continuous plots look truncated just short of the specified
range.

Harlan Harris

unread,
Sep 29, 2009, 7:48:39 AM9/29/09
to ggplot2
Right, this is a slightly unintuitive aspect of ggplot. The scale_
(limits=) syntax restricts the *data* (as do its unfortunately named
synonyms xlim and ylim), while the coord_(xlim=) syntax restricts the
*viewport*. Scale_ functions affects the mapping between data and
plot, while coord_ functions just change the plot. In retrospect, some
of these functions and parameters perhaps should have been named
something else...

-Harlan

hadley wickham

unread,
Sep 29, 2009, 10:15:08 AM9/29/09
to Harlan Harris, ggplot2
> Right, this is a slightly unintuitive aspect of ggplot. The scale_
> (limits=) syntax restricts the *data* (as do its unfortunately named
> synonyms xlim and ylim)

But how could the scales work any other way? If you set
scale_colour_discrete(limits = c("a", "b")) you don't expect "c" and
"d" to some how influence the display of the plot, do you?

> while the coord_(xlim=) syntax restricts the
> *viewport*. Scale_ functions affects the mapping between data and
> plot, while coord_ functions just change the plot. In retrospect, some
> of these functions and parameters perhaps should have been named
> something else...

Possibly, but unless you come up with completely novel names,
everything already has some existing meaning to confuse people.

Hadley

--
http://had.co.nz/

Harlan Harris

unread,
Sep 29, 2009, 11:21:00 AM9/29/09
to ggplot2
On Sep 29, 10:15 am, hadley wickham <h.wick...@gmail.com> wrote:
> > Right, this is a slightly unintuitive aspect of ggplot. The scale_
> > (limits=) syntax restricts the *data* (as do its unfortunately named
> > synonyms xlim and ylim)
>
> But how could the scales work any other way?  If you set
> scale_colour_discrete(limits = c("a", "b")) you don't expect "c" and
> "d" to some how influence the display of the plot, do you?

You don't, and when I think about it, I don't, but people who aren't
experts with ggplot don't yet have a clear understanding of what
"scale" does. Also, it's the first graphing system I've ever used that
lets you restrict the data that way (versus a "subset" type of
command). So, if you're trying to map ggplot's syntax onto your
existing mental models of graphing programs, you're going to assume
that scale_x_continuous(limits=c(1,10)) is going to affect the axis,
not the data. It says "x", and doesn't say "data", so that's a pretty
natural assumption! And the xlim() function is even more likely to be
mapped that way in your head.

>
> > while the coord_(xlim=) syntax restricts the
> > *viewport*. Scale_ functions affects the mapping between data and
> > plot, while coord_ functions just change the plot. In retrospect, some
> > of these functions and parameters perhaps should have been named
> > something else...
>
> Possibly, but unless you come up with completely novel names,
> everything already has some existing meaning to confuse people.

I agree! Not sure what the solution is...

-Harlan

hadley wickham

unread,
Sep 29, 2009, 11:35:11 AM9/29/09
to Harlan Harris, ggplot2
> You don't, and when I think about it, I don't, but people who aren't
> experts with ggplot don't yet have a clear understanding of what
> "scale" does. Also, it's the first graphing system I've ever used that
> lets you restrict the data that way (versus a "subset" type of
> command). So, if you're trying to map ggplot's syntax onto your
> existing mental models of graphing programs, you're going to assume
> that scale_x_continuous(limits=c(1,10)) is going to affect the axis,
> not the data. It says "x", and doesn't say "data", so that's a pretty
> natural assumption! And the xlim() function is even more likely to be
> mapped that way in your head.

I agree it's a confusing, but I'm not sure what to do about it. It's
mainly a problem because the grammar incorporates statistical
transformations, and changes to the data implied by the scale must be
applied before the statistical transformation (which is why it isn't
so much a problem for geoms with the identity stat).

Hadley

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages