diverging size scale ggplot2

我只是一个虾纸丫 提交于 2019-12-12 00:46:36

问题


I am trying to map model/obs differences at locations in space. I have color mapped to the diff but I would also like to map size to diff. i'm currently mapping size to abs(diff) but it produces two different legends that I would like to combine. mapping size to diff creates small points for negative values and large points for positive values but I really only want the magnitude represented for over/under predictions. In case it makes a difference, I would also like the scales to be discrete. Thanks

Some test data:

so.df=data.frame(lat=runif(50,33,43),long=runif(50,-112,-104),diff=runif(50,-2,2))
    ggplot()+
        geom_point(data=so.df,aes(x=long,y=lat,color=diff,size=abs(diff)),alpha=0.8)+
scale_color_gradient2(low='red',mid='white',high='blue',limits=c(-0.6,0.6),breaks=c(-0.6,-0.4,-0.2,-0.1,0,0.1,0.2,0.6))+
        guides(color=guide_legend('SWE Differences (m)'),size=guide_legend('SWE Difference\nMagnitudes (m)'))+
        coord_cartesian(xlim=c(-112.5,-104.25),ylim=c(33,44))+
        theme_bw()

EDIT: to use a discrete color scale I'm using the following (open to suggestions)

so.df$cuts=cut_interval(so.df$diff,length=0.15)
ggplot()+
    geom_path(data=states,aes(x=long,y=lat,group=group),color='grey10')+
    geom_point(data=so.df,aes(x=long,y=lat,color=cuts,size=abs(diff)),alpha=0.8)+
    coord_cartesian(xlim=c(-112.5,-104.25),ylim=c(33,44))+
    scale_colour_brewer(type='div',palette='RdYlBu')+
    guides(color=guide_legend('SWE Differences (m)'),size=guide_legend('SWE Difference\nMagnitudes (m))+ 
    theme_bw()

回答1:


If you map your sizes to your cuts column and use scale_size_manual you can get the diverging size scale you want.

ggplot() + geom_point(data=so.df,aes(x=long,y=lat,size=cuts,color=cuts)) 
+ scale_size_manual(values =   
c(8,6,4,2,1,2,4,6,8),guide="legend") + 
coord_cartesian(xlim=c(-112.5,-104.25),ylim=c(33,44)) +   
scale_colour_brewer(type='div',palette='RdBu')


来源:https://stackoverflow.com/questions/20035359/diverging-size-scale-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!