分歧尺度ggplot2

时间:2013-11-17 19:53:49

标签: r ggplot2 scale legend

我正在尝试在太空中的位置绘制模型/障碍物差异。我有颜色映射到差异,但我也想将大小映射到差异。我目前正在将大小映射到abs(diff),但它产生了两种不同的传说,我想将它们结合起来。将大小映射到diff会为负值创建小点,为正值创建大点,但我实际上只想要用于高于/低于预测的幅度。如果它有所作为,我也希望尺度是离散的。 感谢

一些测试数据:

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()

编辑: 使用离散色标我正在使用以下(开放给建议)

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 个答案:

答案 0 :(得分:1)

如果您将尺寸映射到剪切列并使用scale_size_manual,则可以获得所需的分歧尺寸。

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')