简体   繁体   中英

change legend symbols from boxplot in ggplot2

I am plotting a series of boxplots as well as points on top but my color legend shows all the symbols as little boxes. I've tried using override.aes but the problem persists. Bonus points if you can show me how to make r^2 appear nicely in the title.

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)

First, to remove boxplot "shapes" from the legend you should add show_guide=FALSE to two geom_boxplot() calls. Then yo change shapes in legend with override.aes= you should place it inside guide_legend() (in your try it was placed as separate argument inside function guides() ).

Second, in your title call change order of functions paste() and 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='')))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM