user3173412
user3173412

Reputation: 153

fixed alpha scale ggplot2

I have the following plot: enter image description here

I'm using facet_wrap in ggplot2 to plot multiple groups, on a single pdf page. A sample of the dataset:

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

In each plot I want lines where Source is B_Adj or B, and points when Source is T. I'm using wt to scale the size of points, and I want days to control the transparancy of the points. My problem is when I plot over multiple pages. The legend and transparency seem to relate to the range of values in each "facet". days will always take on values 0:7, and I would like to display the full scale on each page, and also the transparancy level consistent on all pages. As of right now, one page will display an alpha legend consisting of numbers 2 and 3, while another page will have 0 to 6, all depending on what the range is for the groups on that page. Similarly, days=2 will have different transparency on different pages. The same could possibly apply to size?

Code:

    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)

I can`t figure out how to get the guide/legend "DaysOld" on the right to display 0-7, and correspondingly 0 being "full color", 7 being transparent, consistently through the pages.

    > 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     

Upvotes: 2

Views: 3222

Answers (1)

tonytonov
tonytonov

Reputation: 25608

I make a factor and specify breaks and limits for scale_alpha_discrete. You now should have consistency in alpha and its legend across all facets.

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

Upvotes: 4

Related Questions