Re: is it possible to use vieport() with facet_wrap()?

32 views
Skip to first unread message

strunin dmytro

unread,
Nov 30, 2012, 4:33:27 PM11/30/12
to ggp...@googlegroups.com
Sorry, there is mistake in the script I posted before. I changed the variable "time" to "time.points". Now it must work without any problems. Don't know why I can't attach the script file to this letter. So you can download it from here https://docs.google.com/open?id=0BwNQu9iNenS4SDBtOTc3Nk85b2M

Dmytro.

Пʼятниця, 30 листопада 2012 р. 15:52:42 UTC+1 користувач strunin dmytro написав:
Dear R/ggplot2 users!

Could you explain me please, if it is possible to plot subplots on chart created with facet_wrap()? I'm trying to create density plots from the set of FACS experiments. As far as I have 12 time points for treated and untreated samples, I'm trying to find the way how to visualize the data in more compact, readable way. So I'd like to plot density plots for treated samples as usual, using facet_wrap() function, and untreated samples -- as an subplots.  I'm not sure that script below is written clearly enough, but probably it could better explain what I mean:

require(ggplot2)
require(grid)
#This function produce some "flow cytometry"-like data, as a set of mixed normal distributions
fake_facs<-function(){
    means<-c(100,150,200)
    n<-5000
    ind<-sample(1:3,n,replace=T,prob=c(rnorm(1,0.4,0.01),rnorm(1,0.1,0.025),rnorm(1,0.1,0.01)))
    sds<-c(10,20,10)
    x<-rnorm(n,mean=means[ind],sd=sds[ind])
    return(x)
    }
##
##Ploting the data
##
plot.facs<-function(data_set){
    df<-data.frame(x=unlist(data_set),
        grp=rep(time,times=sapply(data_set,length)))
        df$grp<-factor(df$grp,levels=time)
        p<-ggplot(df,aes(x=x,fill=grp))+facet_wrap(~grp,scales="free")+geom_density(linetype=0,alpha=.95)+
            scale_x_continuous(name="")+
            scale_y_continuous(name="Count")+
            theme(axis.ticks = element_blank(), axis.text.x = element_blank(),axis.text.y=element_blank())+
            theme(legend.position="none")+
            coord_equal()
    return(p)
    }
##Experiment simulation
exp.sim<-function(m){
    exp<-list()    #experimental data
    ctrl<-list()    #control data
    time<<-letters[1:m]    #time points
        for(i in time){
            exp[[i]]<-fake_facs()
            ctrl[[i]]<-fake_facs()
            }
vp <- viewport(width = 0.2, height = 0.2, x =0.9,y = 0.9, just=c("right","top"))
print(plot.facs(exp))
return(print(plot.facs(ctrl),vp=vp))
}


The function exp.sim(m) takes the argument m as a number of time points and simulates the data using fake_facs() function. Then I call plot.facs(data_set) function from exp_sim(m) function to plot simulated experimental data for main plot and control data for subplots.

The viewport() works fine if the exp.sim() function takes m=1. But with m>1 (and >26 :) ) it does not work properly. I'm sure that there is my mistake somewhere but I can't see it probably because of lack of experience with R, and probably because of lack of understanding of it.  I would be very grateful to you for any help or suggestion.

Kind regards,

Dmytro
Reply all
Reply to author
Forward
0 new messages