qplot boxplot and ylim

100 views
Skip to first unread message

pdorai

unread,
Jun 12, 2014, 7:29:03 PM6/12/14
to ggp...@googlegroups.com
Hi
I am a newbie to R and am trying to use qplot to generate boxplots.  I am trying to use ylim to set a common display scale for multiple plots.  However, when I use that the box plot I get is different.  Does ylim also restrict the data that is used to create the boxplot or is it only to change the visual display of the axis?

Here is an example:
x <- rep("Dec",6)
y <- c(3,2.3,3.1,16,16.5,20)
d <- data.frame(x,y)
qplot(x,y,data=d,geom="boxplot")
qplot(x,y,data=d,geom="boxplot",ylim=c(0,8))

Phillip Burger

unread,
Jun 16, 2014, 2:58:32 AM6/16/14
to ggp...@googlegroups.com




I don't know the function well enough to explain the behaviour but am replying with a tip that is helpful in situations like this. I'm also adding an example.

A good practice is to use the max() of your measure for the upper limit on the scale:

x <- rep("Dec",6)
y <- c(3, 2.3, 3.1, 16, 16.5, 20)
d <- data.frame(x,y)
qplot(x,y,data=d, geom="boxplot", ylim=c(0,max(y)))

I'm hoping that a more knowledgeable member of the group can answer your question and explain the design decisions. To spur conversation, I've attached an example of a Cleveland plot of the count of flights for each day of the week (from the airline on-time statistics dataset). This plot shows the same behaviour as the plot that you included. Here, I set upper limit to 90,000. The values for days Monday and Sunday exceed 90,000 and the marks are not plotted.

-Phil
not-plotting-if-value-beyond-axis-limit.png

Ito, Kaori (Groton)

unread,
Jun 16, 2014, 9:04:58 AM6/16/14
to pdorai, ggp...@googlegroups.com

If you want to set up a common display scale, make sure that the limit covers the range of possible data in each panel.

If you use limits, and if there is data point(s) outside the range of the limits, those data points are dropped.

In your example data,  

y <- c(3,2.3,3.1,16,16.5,20)
16, 16.5, 20 are dropped if you set up ylim=c(0,8)

 

This is equivalent to:

qplot(x,y,data=d,geom="boxplot") + scale_y_continuous(limits=c(0,8))

 

 

If you don’t want to drop the data but just zoom in the area, use coord_carterian instead (see the difference in output).

qplot(x,y,data=d,geom="boxplot") + coord_cartesian(ylim=c(0,8))

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

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pdorai

unread,
Jun 16, 2014, 4:26:23 PM6/16/14
to ggp...@googlegroups.com, pdo...@gmail.com
Thanks very much!  Some of my data have huge outliers that if I use ymax to come up with ylim, it just makes the box plot so much smaller.  That's why I wanted to zoom in.  Your last sentence is what I was looking for.  Thanks!
Reply all
Reply to author
Forward
0 new messages