【发布时间】:2015-05-02 21:34:36
【问题描述】:
举个例子:
set.seed(1)
tmp.data<-data.frame(group=rep(c("x","y","z"),8),
year=rep(c(2000:2003),6),
value=runif(24, 1, 100))
我可以创建一个带有团体隶属关系的简单箱线图:
boxplot.example<-ggplot(data=tmp.data)
boxplot.example.simple<-boxplot.example +
geom_boxplot(aes(x=group,y=value))
# plot
boxplot.example.simple
但是,我想在同一图形中为每个组和年份创建单独的箱线图。
我用 ggplot 的 group 函数试了一下:
boxplot.example.yearly<-boxplot.example +
geom_boxplot(aes(x=year,y=value, group=group))
# plot
boxplot.example.yearly # does not work as expected
但是,分组没有按预期工作。
然后我尝试像这样使用split 和llply:
require("plyr")
boxplot.example.yearly.2<-ggplot() +
llply(.data=split(tmp.data,tmp.data$year),.fun=geom_boxplot,
aes(x=year,y=value))
# Error: ggplot2 doesn't know how to deal with data of class uneval
这可能是由于数据参数未在 ggplot 函数中指定。
那么如何将箱线图绘制成一个按group 和年度观察分组的图表?
【问题讨论】:
标签: r plot ggplot2 subset plyr