geom_pointrange and limits on scale_y_continuous

237 views
Skip to first unread message

David Morrison

unread,
Sep 18, 2012, 4:46:51 PM9/18/12
to ggp...@googlegroups.com
I recently plotted some data using geom_pointrange and was surprised at how that geom behaved when I set limits on the scale.  This code should illustrate things:

d<-data.frame(x=c(1,2),y=c(0.01,0))
p<-ggplot(d)+geom_pointrange(aes(x=x,y=y,ymin=y-0.1,ymax=y+0.1))+scale_y_continuous(limits=c(-0.095,0.11))
print(p)

The limits on the scale are such that they clip the ymin of the second data point.  I had figured I would end up with an error bar extending to the lower edge of the plot.  Failing that, I figured the geom for that data point would be removed entirely from the plot.  What I got was surprisingly in between those two extremes.  The error bar was gone but the central value was plotted.  In my case, this led to several minutes of confusion as I tried to figure out what was going on.

I think the appearance of a geom should be "atomic"; it's either there or it isn't.  For points, this is natural, but for extended geoms, I guess one could debate what the behavior ought to be. Should you show the portion of the geom within the range of the plot (in other words, have a behavior like you'd get with coord_cartesian) or should the geom be gone entirely?  Plotting part of the geom (in this case, just the central value) seems like the wrong thing to do.

Kohske Takahashi

unread,
Sep 26, 2012, 6:28:32 PM9/26/12
to David Morrison, ggp...@googlegroups.com
At least that shows working message such as
>Removed 1 rows containing missing values (geom_segment).
in the development version.

kohske

2012/9/19 David Morrison <da...@bnl.gov>:
> --
> 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



--
Kohske Takahashi <takahash...@gmail.com>

Assistant Professor,
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Hadley Wickham

unread,
Sep 27, 2012, 3:26:22 AM9/27/12
to David Morrison, ggp...@googlegroups.com
On Tue, Sep 18, 2012 at 10:46 PM, David Morrison <da...@bnl.gov> wrote:
> I recently plotted some data using geom_pointrange and was surprised at how
> that geom behaved when I set limits on the scale. This code should
> illustrate things:
>
> d<-data.frame(x=c(1,2),y=c(0.01,0))
> p<-ggplot(d)+geom_pointrange(aes(x=x,y=y,ymin=y-0.1,ymax=y+0.1))+scale_y_continuous(limits=c(-0.095,0.11))
> print(p)
>
> The limits on the scale are such that they clip the ymin of the second data
> point. I had figured I would end up with an error bar extending to the
> lower edge of the plot. Failing that, I figured the geom for that data
> point would be removed entirely from the plot. What I got was surprisingly
> in between those two extremes. The error bar was gone but the central value
> was plotted. In my case, this led to several minutes of confusion as I
> tried to figure out what was going on.

The easiest way to understand the behaviour is to think about the data
- all data outside the limits is discarded.

I don't think there's anyway to think about the "atom" of geoms - I
think it would be even more confusing if restricting the limits on a
line removed all points if one was missing.

Hadley


--
RStudio / Rice University
http://had.co.nz/

David Morrison

unread,
Sep 27, 2012, 11:21:23 PM9/27/12
to ggp...@googlegroups.com, David Morrison
I don't know.  Would it be confusing if the entire line disappeared if one point fell outside the limits of the scale?  Maybe, maybe not, but it also seems to be a moot point, as that is exactly what does happen:

 d<-data.frame(x=c(1,2,3),y=c(1,-1,1))
 p<-ggplot(d)+geom_line(aes(x=x,y=y))+scale_y_continuous(limits=c(0,2))
 print(p)


 But let me try a different example:

 d<-data.frame(x=c(1,2),y=c(0,0))
 p<-ggplot(d)+geom_errorbar(aes(x=x,y=y,ymin=y-0.1,ymax=y+0.1))+scale_x_continuous(limits=c(0.5,2.25))
 print(p)


Now the crossbar at each end of the error bar completely disappears because one end of it falls just beyond the scale limit.  I'm not sure how I understand this behavior by focusing on the data.  Do you consider the crossbars to be data? 

I guess my point is that there is already some concept of atomicity implicit in the way ggplot2 draws geoms, it's just not consistent.  A point is either there or it's not.  Same for a line.  Not so for pointranges and errorbars, maybe others.  I think consistency in this regard would be a *good* thing, right?

Dave

Hadley Wickham

unread,
Sep 28, 2012, 12:24:27 AM9/28/12
to David Morrison, ggp...@googlegroups.com
> I don't know. Would it be confusing if the entire line disappeared if one
> point fell outside the limits of the scale? Maybe, maybe not, but it also
> seems to be a moot point, as that is exactly what does happen:
>
> d<-data.frame(x=c(1,2,3),y=c(1,-1,1))
> p<-ggplot(d)+geom_line(aes(x=x,y=y))+scale_y_continuous(limits=c(0,2))
> print(p)

Unfortunately you picked an example with only three points, and the
missing point was in the middle.

d <- data.frame(x = 1:10, y = c(1:5, 5:1))
ggplot(d, aes(x, y)) + geom_line() + ylim(1,4)


> But let me try a different example:
>
> d<-data.frame(x=c(1,2),y=c(0,0))
>
> p<-ggplot(d)+geom_errorbar(aes(x=x,y=y,ymin=y-0.1,ymax=y+0.1))+scale_x_continuous(limits=c(0.5,2.25))
> print(p)
>
> Now the crossbar at each end of the error bar completely disappears because
> one end of it falls just beyond the scale limit. I'm not sure how I
> understand this behavior by focusing on the data. Do you consider the
> crossbars to be data?

Yes!

David Morrison

unread,
Sep 29, 2012, 11:59:05 AM9/29/12
to ggp...@googlegroups.com, David Morrison
I guess I'm destined to lose this argument, but let me gamely take one last crack at it! OK, even though nothing in my data set specified anything about the length of the endcaps on my errorbars, and I because of that I think of them as decoration, I should instead think of them as data and I shouldn't be surprised that they disappear entirely when they stretch beyond the limit specified in my scale.  OK–deep breath–I've internalized that. Got it. Now I'm going to plot some data with big round symbols, the size of which is explicitly controlled by my data set.  Should I expect a plotted point that stretches beyond my scale limits to disappear or not?

 d<-data.frame(x=c(1,3),y=c(1,1),z=c(10,100))
 p<-ggplot(d)+geom_point(aes(x=x,y=y),pch=21,size=d$z)+scale_x_continuous(limits=c(0,3))
 print(p)


(Just so there's no suspense, the second point does not disappear, the portion of the circle within the limits of the scale is drawn.)

Hadley Wickham

unread,
Oct 2, 2012, 10:18:09 AM10/2/12
to David Morrison, ggp...@googlegroups.com
> specified in my scale. OK–deep breath–I've internalized that. Got it. Now
> I'm going to plot some data with big round symbols, the size of which is
> explicitly controlled by my data set. Should I expect a plotted point that
> stretches beyond my scale limits to disappear or not?

Yes, that's an inconsistency, basically due to technical limitations of grid.
Reply all
Reply to author
Forward
0 new messages