Re: How to make the width of boxplot proportional to the sample size??

2,041 views
Skip to first unread message

Roman Luštrik

unread,
Oct 9, 2012, 2:41:01 AM10/9/12
to zxlsupergirl, ggp...@googlegroups.com
Try whipping up a reproducible example and try reproducing the advice in the link. If that doesn't work, give us a whistle.

Cheers,
Roman



On Tue, Oct 9, 2012 at 12:04 AM, zxlsupergirl <zxlsupe...@gmail.com> wrote:
Hi All,

I have difficulties to make the width of boxplot proportional to the sample size using ggplot.

I didn't use geom_box(). Instead, I used (Because I redefined min and max) :
ggplot(stat, ...)+
geom_errorbar(aes(ymin=min,ymax=Q1, ...)+
geom_errorbar(aes(ymin=Q3,ymax=max, ...)+
geom_crossbar(aes(y=median,ymin=Q1,ymax=Q3, ...)

But it seems width can only equal to constant number instead of vector.....

I need to make multi-panel plots, so I also used   facet_grid().

But it didn't work for me......(I may did something wrong..)

Any suggestion would be appreciated!!

Shirley

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



--
In God we trust, all others bring data.

zxlsupergirl

unread,
Oct 9, 2012, 8:41:11 AM10/9/12
to ggp...@googlegroups.com, zxlsupergirl
Thanks, Roman!

I was trying to reproduce the advice as following:
1) change proto(Geom to proto(ggplot2:::Geom for geom_boxplot; similarly change proto(Stat to proto(ggplot2:::Stat for stat_boxplot.

2) In function 'calculate' in stat_boxplot, change:
if (length(unique(x)) > 1) width <- diff(range(x)) * 0.9 to width <- length(y);

In function 'reparameterise' in geom_boxplot, change:
df$width <- df$width %||%params$width %||% (resolution(df$x, FALSE) * 0.9) to df$width <- df$width*(resolution(df$x, FALSE) * 0.9)/max(df$width);

3) Source 'geom-boxplot.r' and 'stat-boxplot.r';

4) run. change the 'objname' to, say, 'boxplot2'. When calling geom_boxplot, I needed to explicitly specify the stat:
p <- ggplot(mtcars, aes(factor(cyl), mpg)) 
p + 
geom_boxplot(stat='boxplot2')

I think I did right for the first three steps, but for the last step, when I run p + geom_boxplot(stat='boxplot2'), I always get error: "Error: No stat called boxplot2"

Here is what I did for step4: objname <- "boxplot2" (I changed it for both geom_boxplot and stat_boxplot. is that correct?)

If I change to "boxplot2", do I also need to change the highlight part to "boxplot2"?
geom_boxplot <- function (mapping = NULL, data = NULL, stat = "boxplot", position = "dodge", .....
stat_boxplot<- function (mapping = NULL, data = NULL, geom = "boxplot", position = "dodge", 


Thanks so much!!

zxlsupergirl

unread,
Oct 9, 2012, 2:50:11 PM10/9/12
to ggp...@googlegroups.com, zxlsupergirl
Is there anyone who tried this procedure of making changes to geom_boxplot and stat_boxplot??

This example can be used for testing.

p <- ggplot(mtcars, aes(factor(cyl), mpg)) 
p + 
geom_boxplot(stat='boxplot2')

Tomas Sieger

unread,
Oct 31, 2013, 5:47:23 AM10/31/13
to ggp...@googlegroups.com
Hi all,
 I implemented boxplots of variable width (proportional to the square root of the number of observations in each group):
https://github.com/tsieger/ggplot2/tree/boxplot_varwidth

If you wish, you can give it a try, e.g. using:

 library(devtools)
 install_github('ggplot2','tsieger','boxplot_varwidth')
 library(ggplot2)

 p <- ggplot(mtcars, aes(factor(cyl), mpg))
 p + geom_boxplot(varwidth = TRUE)

BTW I also created a pull request in the original repository:
https://github.com/hadley/ggplot2/pull/868

Hope this helps.

Tomas


Tomas Sieger

unread,
Nov 1, 2013, 5:10:18 AM11/1/13
to ggp...@googlegroups.com
Hi all,
 I implemented boxplots of variable width (proportional to the square root of the number of observations in each group):
https://github.com/tsieger/ggplot2/tree/boxplot_varwidth

If you wish, you can give it a try, e.g. using:

 library(devtools)
 install_github('ggplot2','tsieger','boxplot_varwidth')
 library(ggplot2)

 p <- ggplot(mtcars, aes(factor(cyl), mpg))
 p + geom_boxplot(varwidth = TRUE)

BTW I also created a pull request in the original repository:
https://github.com/hadley/ggplot2/pull/868

Hope this helps.

Tomas


On Tue, Oct 9, 2012 at 8:50 PM, zxlsupergirl <zxlsupe...@gmail.com> wrote:

T

unread,
Nov 10, 2017, 6:06:05 AM11/10/17
to ggplot2
Hi Tomas,

After installing the git hub package and executing the code in your mtcars example,  I can't plot box plot any more because R returns me the error and warning message below. I updated ggplot2 package but it still didn't work. I don't know what happened. How can I recover the original ggplot2 package?

Thank you for your reply.

Error in DiscreteRange$new() : `inherit` must be a R6ClassGenerator.
In addition: Warning messages:
1: In structure(NULL, class = "waiver") :
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.
2: In structure(NULL, class = "waiver") :
  Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
  Consider 'structure(list(), *)' instead.

Best wishes,
T
Reply all
Reply to author
Forward
0 new messages