[R] Problems with boxplot in ggplot2:qplot

Brian Diggs diggsb at ohsu.edu
Tue Feb 16 22:54:37 CET 2010


On 2/15/2010 2:41 PM, Dimitri Shvorob wrote:
> library(sqldf)
> library(ggplot2)
> 
> t = data.frame(t = seq.Date(as.Date("2009-01-01"), to =
> as.Date("2009-12-01"), by = "month"))
> x = data.frame(x = rnorm(5))
> df = sqldf("select * from t, x")

A simpler way to get random data that doesn't involve the sqldf package, and gets different x values for each date:

df <- data.frame(t = seq.Date(as.Date("2009-01-01"), to=as.Date("2009-12-01"), by="month"), x=rnorm(60))

> qplot(factor(df$t), df$x, geom = "boxplot") + theme_bw()

You are converting your dates to a factor, so they are no longer dates.  I'm guessing you did this to get a separate boxplot for each date, but that is not the right way to do that.  Use the "group" aesthetic to make different groups.

qplot(df$t, df$x, geom = "boxplot", group=df$t) + theme_bw()

> qplot(factor(df$t), df$x, geom = "boxplot") + theme_bw() +
> scale_x_date(major = "months",  minor = "weeks", format = "%b") 

qplot(df$t, df$x, geom = "boxplot", group=df$t) + theme_bw() +
scale_x_date(major = "months",  minor = "weeks", format = "%b")

> qplot(factor(df$t), df$x, geom = "boxplot") + theme_bw() +
> scale_x_date(format = "%b") 

qplot(df$t, df$x, geom = "boxplot", group=df$t) + theme_bw() +
scale_x_date(format = "%b")

--
Brian Diggs, Ph.D.
Senior Research Associate, Department of Surgery, Oregon Health & Science University



More information about the R-help mailing list