2

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     
tonytonov
  • 25,060
  • 16
  • 82
  • 98
user3173412
  • 143
  • 1
  • 10
  • Variables like `CurveID` and `CurveName` seem to be missing from your data set so your code isn't runnable so we can't see the output either. Please edit to make your example complete. You may upload an image to `imgur.com` and provide a URL (someone with higher rep may edit to embed the image in your post). – MrFlick Jun 12 '14 at 14:36
  • Edited as requested.stig – user3173412 Jun 15 '14 at 22:21

1 Answers1

4

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

tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • Thanks for the suggestion! One of the reasons I went with a continuous scale, was the "trans" parameter. In your solution, the newest observations are most transparent, but I`d like the reverse. The discrete scale does not support this option. – user3173412 Jun 16 '14 at 07:27
  • In that case, use `p + scale_alpha_discrete(range=c(0.5, 1), limits=7:0, breaks=0:7)`. – tonytonov Jun 16 '14 at 07:39
  • Awsome :) Can I apply similar functionality for weight? This one has to be contiuous though. – user3173412 Jun 16 '14 at 07:47
  • It's the same: `+ scale_size_continuous(limits=c(0, 2), breaks=0:4/2)`. – tonytonov Jun 16 '14 at 07:55
  • Thanks, think I get the picture now. You have been most helpful! You would not happen to know how to control the print order of each facet? I do have data on sort order, but I`m not sure how to utilize it. Cheers. – user3173412 Jun 16 '14 at 08:07
  • You are welcome! For facet ordering, see e.g. [this question](http://stackoverflow.com/questions/5490638/how-to-change-the-order-of-facet-labels-in-ggplot-custom-facet-wrap-labels). – tonytonov Jun 16 '14 at 08:09