【问题标题】:change legend symbols from boxplot in ggplot2从ggplot2中的boxplot更改图例符号
【发布时间】:2013-11-15 21:44:34
【问题描述】:

我正在绘制一系列箱线图以及顶部的点,但我的颜色图例将所有符号显示为小框。我试过使用 override.aes 但问题仍然存在。如果你能告诉我如何让 r^2 在标题中很好地出现,那就加分。

r2df.realtime=subset(r2df,yr>reconyear)
r2df.sameyr=subset(r2df,yr==reconyear)
ggplot()+geom_boxplot(data=r2df.realtime,aes(as.factor(yr),r2phvrcn,col='model1'),outlier.shape=3)+
    geom_boxplot(data=r2df,aes(x=as.factor(yr),y=r2phv,col='model2'))+
    geom_point(data=r2df,aes(x=as.factor(yr),y=r2recon,col='model3'),shape=6)+
    geom_point(data=r2df.sameyr,aes(x=as.factor(yr),y=r2phvrcn,col='model4'),shape=6)+
    scale_color_manual(values=c('blue','red','green','black'))+
    facet_grid(~mth)+
    guides(color=guide_legend('Model'),override.aes=list(shape=c(6,6,1,1)))
    labs(title=paste('Real-time Ensemble of Cross-Validated Skill Scores (',expression(r^2),')',sep=''))

link to text file with dput(r2df)

【问题讨论】:

  • 能否将您的代码缩减为显示问题所需的代码?
  • 问题在于复杂性......但我已经删除了一些轴标签等。还有什么不清楚的?

标签: r ggplot2 legend


【解决方案1】:

首先,要从图例中删除箱线图“形状”,您应该将 show_guide=FALSE 添加到两个 geom_boxplot() 调用中。然后你用override.aes=改变图例中的形状,你应该把它放在guide_legend()中(在你的尝试中,它作为单独的参数放在函数guides()中)。

其次,在您的标题调用更改顺序中的函数paste()expression()

ggplot()+
  geom_boxplot(data=r2df.realtime,aes(as.factor(yr),r2phvrcn,col='model1'),
                                           outlier.shape=3,show_guide=FALSE)+
  geom_boxplot(data=r2df,aes(x=as.factor(yr),y=r2phv,col='model2'),show_guide=FALSE)+
  geom_point(data=r2df,aes(x=as.factor(yr),y=r2recon,col='model3'),shape=6)+
  geom_point(data=r2df.sameyr,aes(x=as.factor(yr),y=r2phvrcn,col='model4'),shape=6)+
  scale_color_manual(values=c('blue','red','green','black'))+
  facet_grid(~mth)+
  guides(color=guide_legend('Model',override.aes=list(shape=c(1,1,6,6))))+
  labs(title=expression(paste("Real-time Ensemble of Cross-Validated Skill Scores 
                                                                (",r^2,")",sep='')))

【讨论】:

  • 谢谢,但这并不能完全满足我的需求。我仍然想保留“model1”的箱线图形状。我想为“model2”显示一条线,为“model3”和“model4”显示三角形。 show_guide=T for 'model1' 使所有形状都成为箱线图。我尝试对 model2 使用 geom_segment 而不是 geom_boxplot 但无法使其正常工作。图上的输出是一样的,因为model2只有一个值。
猜你喜欢
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
相关资源
最近更新 更多