固定的alpha scale ggplot2

时间:2014-06-12 08:26:43

标签: r ggplot2

我有以下情节: enter image description here

我在ggplot2中使用facet_wrap在单个pdf页面上绘制多个组。 数据集的示例:

pData=data.frame(CurveId=rep(71,19),
             CurveName=rep("OBOS",19),
             TTM=c(10,7,5,3,2,1,0.5,0.25,10,7,5,3,2,1,0.5,0.25,2.4476,4,3),
             Spread=c(157,136,120,97,76,46,39,34,162,141,125,102,81,50,41,37,64,80,70),
             Source=c(rep("B_Adj",8),rep("B",8),rep("T",3)),
             wt=c(rep(0.5,16),2,1.8,1.6),
             days=c(rep(0,16),1,2,0)
             )

在每个情节中,我想要的是源为B_AdjB的行,以及当源为T时的点。 我使用wt来缩放点的大小,我希望days能够控制点的透明度。我的问题是当我绘制多个页面时。图例和透明度似乎与每个" facet"中的值范围有关。 days将始终采用值0:7,并且我希望在每个页面上显示完整比例,并且还要在所有页面上显示一致的透明度级别。截至目前,一个页面将显示由数字2和3组成的字母图例,而另一个页面将显示0到6,所有这些都取决于该页面上组的范围。同样,days = 2将在不同页面上具有不同的透明度。同样可能适用于尺寸?

代码:

    p=ggplot(pData)+

    geom_point(data=subset(pData,Source %in% c("T")),
         aes(x=TTM,y=Spread,group=Source,shape=Source,colour=Source,size=wt,
             alpha=days),
         shape=16)+

    scale_alpha(name="Days",trans="reverse",range=c(0.2,1))+

    geom_line(data=subset(pData,Source %in% c("B","B_Adj")),
         aes(x=TTM,y=Spread,group=Source,color=Source))+

    guides(group=guide_legend(title="Kilde",order=1),
           alpha=guide_legend(title="DaysOld",order=2),
           size=guide_legend(order=3,title="Weight"))+

    facet_wrap(~CurveName,scales="free_y",ncol=nCol,nrow=nRows)

   print(p)

我无法弄清楚如何获得指南/传奇" DaysOld"在右边显示0-7,相应地0是"全彩",7是透明的,始终贯穿页面。

    > sessionInfo()
    R version 3.1.0 (2014-04-10)
    Platform: i386-w64-mingw32/i386 (32-bit)

    locale:
    [1] LC_COLLATE=Norwegian (Bokmål)_Norway.1252  LC_CTYPE=Norwegian                         (Bokmål)_Norway.1252    LC_MONETARY=Norwegian (Bokmål)_Norway.1252
    [4] LC_NUMERIC=C                               LC_TIME=Norwegian (Bokmål)_Norway.1252    

    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     

    other attached packages:
    [1] ggplot2_1.0.0 RODBC_1.3-10 

    loaded via a namespace (and not attached):
     [1] colorspace_1.2-4 digest_0.6.4     grid_3.1.0       gtable_0.1.2             labeling_0.2     MASS_7.3-31      munsell_0.4.2    plyr_1.8.1       proto_0.3-10    
    [10] Rcpp_0.11.1      reshape2_1.4     scales_0.2.4     stringr_0.6.2    tools_3.1.0     

1 个答案:

答案 0 :(得分:1)

我制作了一个因素并为breaks指定limitsscale_alpha_discrete。你现在应该在所有方面都具有alpha及其图例的一致性。

pData$fdays <- as.factor(pData$days)
p <- ggplot(pData) +
  geom_point(data=subset(pData, Source %in% c("T")),
             aes(x=TTM, y=Spread, group=Source, shape=Source, colour=Source, 
                size=wt, alpha=fdays),
             shape=16) + 
  geom_line(data=subset(pData, Source %in% c("B", "B_Adj")),
            aes(x=TTM, y=Spread, group=Source, color=Source)) +
  guides(group=guide_legend(title="Kilde", order=1),
         alpha=guide_legend(title="DaysOld", order=2),
         size=guide_legend(order=3, title="Weight")) +
  facet_wrap(~CurveName, scales="free_y")

p + scale_alpha_discrete(range=c(0.5, 1), limits=0:7, breaks=0:7)

enter image description here