stat_summary doesn't work if x-variable is factor?

1,729 views
Skip to first unread message

Ito, Kaori (Groton)

unread,
Mar 28, 2012, 2:53:05 PM3/28/12
to ggp...@googlegroups.com

Dear ggplot users,

I am trying to create a box plot with smooth line with the code below, which was successful with a smaller dataset (example plot is below), but I got error when I used the same code but with a larger dataset.

 

image001

 

p <- ggplot(d, aes(time, Resp)) + geom_boxplot()

p + facet_grid(.~trt) + geom_smooth(aes(group = 1))

 

The error message was:

 

Error in smooth.construct.cr.smooth.spec(object, data, knots) :

x has insufficient unique values to support 10 knots: reduce k.

 

So, I googled any help to solve this, and I came to R-help communication using below:

 

 

## summary function ##

stat_sum_single <- function(fun, geom="point", ...) {

   stat_summary(fun.y=fun, colour="red", geom=geom, size = 3, ...)

 }

 

stat_sum_df <- function(fun, geom="crossbar", ...) {

   stat_summary(fun.data=fun, colour="red", geom=geom, width=0.2, ...)

 }

 

 

## example from ggplot web page

c <- qplot(cyl, mpg, data=mtcars)

 c + stat_summary(fun.data = "mean_cl_boot", colour = "red")

c + stat_sum_single(mean, geom="line")

c + stat_sum_df("mean_cl_normal", geom = "smooth")

 

 

However, it seems to me it is not working if x-variable is factor.

 

#if cyl is factor?

c <- qplot(as.factor(cyl), mpg, data=mtcars)

c <- c + geom_boxplot()                             #creat box plot

 c + stat_summary(fun.data = "mean_cl_boot", colour = "red")

c + stat_sum_single(mean, geom="line")              #did't work

c + stat_sum_df("mean_cl_normal", geom = "smooth")  #didn't work

 

If I want to create a box plot overlay with smooth line (or mean line), in this case, x-variable should be factor variable. I also don’t know why geom_smooth(aes(group = 1)) didn’t work (is there any way to reduce “knot”? ).

 Have anyone had the same problem? Any help would be appreciated.

Regards,

Kaori

 

 

Dennis Murphy

unread,
Mar 29, 2012, 12:20:00 AM3/29/12
to Ito, Kaori (Groton), ggp...@googlegroups.com
Hi:

This works for me:

d <- ggplot(mtcars, aes(x = as.factor(cyl), y = mpg)) +
           geom_boxplot() +

           stat_summary(fun.data = 'mean_cl_boot', colour = 'red')
d + stat_sum_single(mean, geom = 'line', aes(group = 1),
                           size = 1, colour = 'red')
d + stat_sum_df("mean_cl_normal", geom = "smooth", aes(group = 1))

# or, putting the last two commands together:
d + stat_sum_df("mean_cl_normal", geom = "smooth", aes(group = 1), size = 1)

# Here's a more roundabout way, where the factor levels or their numeric equivalents
# are specified in each layer:

d <- ggplot(mtcars, aes(y = mpg)) +
           geom_boxplot(aes(x = as.factor(cyl))) +
           stat_summary(fun.data = 'mean_cl_boot', colour = 'red', aes(x = as.factor(cyl)))
# Convert the factor levels to numeric for the line and smooth:
d + stat_sum_single(mean, geom = 'line', aes(x = as.numeric(as.factor(cyl))),
                           size = 3, colour = 'red')
d + stat_sum_df("mean_cl_normal", geom = "smooth", aes(x = as.numeric(as.factor(cyl))))

Dennis

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

image001.png

Ito, Kaori (Groton)

unread,
Mar 29, 2012, 1:41:17 PM3/29/12
to Dennis Murphy, ggp...@googlegroups.com

Hi Dennis,

You saved me again…thank you!!

Kaori

Reply all
Reply to author
Forward
0 new messages