【发布时间】:2016-01-27 23:30:01
【问题描述】:
我的名字是 Giacomo,我只是一个 R 初学者。 我正在尝试生成由两条趋势线覆盖的箱线图图形,每条趋势线用于我绘制的每个类。
谷歌搜索我发现了很多例子,像这样 ggplot - Add regression line on a boxplot with binned (non-continuous) x-axis 但它对我不起作用。
最后我尝试了两种不同的方法,第一种是:
plotSerie <- ggplot(fileIn, aes(y=S1_VH)) +
geom_boxplot(aes(x=as.factor(DOY), fill = X2cycles)) +
geom_smooth(method="loess", se=TRUE, aes(x=as.integer(DOY), color=X2cycles)) +
scale_fill_manual(values=c("ShortCycle"= "brown", "LongCycle" = "grey"),
name="Rice Cycles")+
scale_color_manual(values=c("ShortCycle"= "brown", "LongCycle" = "grey"),
name="Rice Cycles")+
labs(x = "DOY", y = "VH")+
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20,face="bold"),
legend.text=element_text(size=20),
legend.title=element_text(size=25))+
ylim(-24,-14)
优点:两条趋势线都正确显示, 缺点:箱线图和趋势线没有叠加
第二种方式是
plotSerie <- ggplot(fileIn, aes(x=factor(DOY), y=S1_VH, fill = X2cycles))+
geom_boxplot() +
geom_smooth(method="loess", se=TRUE, aes(group=1, color=X2cycles)) +
scale_fill_manual(values=c("ShortCycle"= "brown", "LongCycle" = "grey"),
name="Rice Cycles")+
scale_color_manual(values=c("ShortCycle"= "brown", "LongCycle" = "grey"),
name="Rice Cycles")+
labs(x = "DOY", y = "VH")+
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20,face="bold"),
legend.text=element_text(size=20),
legend.title=element_text(size=25))+
ylim(-24,-14)
优点:箱线图和一条趋势线正确叠加, 缺点:只绘制了一条趋势线
你能帮帮我吗? 非常感谢
【问题讨论】:
-
1.请发布一个最小的、独立的示例。 2.请阅读手册(
?geom_boxplot)。 “只要提供分组变量,您也可以使用带有连续 x 的箱线图。” -
@Henrik:非常感谢。我仔细阅读了手册,但我是初学者,我无法解决我的问题(现在)。 fileIn 是我的数据框的名称。我怎样才能附上这个文件,或者我怎样才能给你这个文件?再次感谢您
-
我解决了这个问题。很容易。这是解决方案: geom_smooth(method="loess", se=TRUE, aes(group=X2cycles, color=X2cycles)) +