2016-01-25 35 views
0

我的名字是Giacomo,我只是一个R初学者。 我正试图制作一张由两条趋势线覆盖的箱形图,一个用于绘制每个课程。ggplot&geom_boxplot的老问题&geom_smooth

使用谷歌搜索我发现很多例子,像这样 ggplot - Add regression line on a boxplot with binned (non-continuous) x-axis 但它不适用于我。

在我tryied两种不同的方式结束时,第一个是:

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) 

优点:无论是趋势线被正确地示出, 缺点:箱线图和趋势线不overlayied

第二种方法是

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)                      

优点:箱线图和一个趋势线被正确overlayied, 缺点:只有一个趋势线绘制

你能帮我吗? 非常感谢你

+0

什么在'fileIn' – mtoto

+2

1.请发表_minimal,自contained_例子。 2.请阅读手册('?geom_boxplot')。 “只要你提供一个分组变量,你也可以使用连续x的盒子图。” – Henrik

+0

@亨利克:非常感谢。我仔细阅读手册,但我是一个初学者,我无法解决我的问题(现在)。 fileIn是我的数据框的名称。我怎样才能附加这个文件,或者我怎样才能为你提供这个文件?再次谢谢 – ilFonta

回答

0

我解决了这个问题。不管怎样,谢谢你。

plotSerie <- ggplot(fileIn, aes(x=factor(DOY), y=S1_VH, fill = X2cycles))+ 
    geom_boxplot() + 
    geom_smooth(method="loess", se=TRUE, aes(group=X2cycles, 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

正如已经在[您发布的文章中指出的](http://stackoverflow.com/questions/21296789/ggplot-add-regression-line-on-a-boxplot-with-binned-non-continuous-x-axis)“_it is a stunningly bad idea_”and“_can be misleading_”to add a smoother when x轴是离散的。我相信,使用'?geom_boxplot'中描述的方法连续使用boxlot,会更好。 – Henrik

+0

感谢您的反馈。祝你好运! – Henrik

0

我为我的延误表示歉意。我改变了我的脚本,以便在我的情节中使用连续的X.

plotSerie <- ggplot(fileIn, aes(x=DOY, y=S1_VH, fill = pass, group=DOY))+ 
    geom_boxplot() + 
    geom_smooth(method="loess", se=TRUE, aes(group=pass, color=pass)) +  

非常感谢您