【问题标题】:old issue with ggplot & geom_boxplot & geom_smoothggplot & geom_boxplot & geom_smooth 的老问题
【发布时间】: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)) +

标签: r ggplot2


【解决方案1】:

我解决了这个问题。还是谢谢你。

    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)                                                                                      

【讨论】:

  • 正如the post you refer to 中已经指出的那样,“这是一个非常糟糕的主意”和“可能会误导” x 轴是离散的。我相信最好使用?geom_boxplot 中描述的方法使用带有连续 x 的箱线图
  • 感谢您的反馈。祝你的阴谋好运!
【解决方案2】:

对于我的延误,我深表歉意。为了在情节中使用连续 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)) +    

非常感谢

【讨论】:

    猜你喜欢
    • 2020-04-03
    • 2017-02-11
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多