[R] Ggplot: free x-scales in a facet-grid

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Fri Feb 11 12:26:22 CET 2011


Dear Ann,

The easiest way is to seperate both plots into two subplots. And then use viewport to paste them together.

Best regards,

Thierry


p1 <- 
	ggplot(subset(data.melt, pos = "FALSE"),aes(value,ID)) +
geom_point(aes(groups=time,colour=time,shape=time)) +
facet_grid(type~.,scales="free",space="free") +
opts(strip.text.y=theme_text())+
xlab(NULL) + ylab(NULL)+
opts(axis.text.x = theme_text(angle = 90, hjust = 1, size = 8)) +
opts(legend.text = theme_text(hjust=1, size = 8))+
opts(legend.position="top",legend.direction="horizontal")+
scale_shape_manual(values = c(1,3,0,2),name="")  +
scale_colour_manual(values =
c("red","red","royalblue4","mediumvioletred"),name="")

p2 <- 
	ggplot(subset(data.melt, pos = "TRUE"),aes(value,ID)) +
geom_point(aes(groups=time,colour=time,shape=time)) +
facet_grid(type~.,scales="free",space="free") +
opts(strip.text.y=theme_text())+
xlab(NULL) + ylab(NULL)+
opts(axis.text.x = theme_text(angle = 90, hjust = 1, size = 8)) +
opts(legend.text = theme_text(hjust=1, size = 8))+
opts(legend.position="top",legend.direction="horizontal")+
scale_shape_manual(values = c(1,3,0,2),name="")  +
scale_colour_manual(values =
c("red","red","royalblue4","mediumvioletred"),name="")

vp1 <- viewport(width = 1/3, height = 1, x = 1/6, y = 0.5)
vp2 <- viewport(width = 2/3, height = 1, x = 4/6, y = 0.5)
print(p1, vp = vp1)
print(p1, vp = vp2)


----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium

Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium

tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
  

> -----Oorspronkelijk bericht-----
> Van: r-help-bounces op r-project.org 
> [mailto:r-help-bounces op r-project.org] Namens Strategische 
> Analyse CSD Hasselt
> Verzonden: vrijdag 11 februari 2011 10:09
> Aan: r-help op r-project.org
> Onderwerp: Re: [R] Ggplot: free x-scales in a facet-grid
> 
> Hello,
> 
> hereby the code with example data, as an attach to my 
> question (see mail 
> below).
> Thank you!
> Ann
> 
> library(ggplot2)
> library(grid)
> library(RColorBrewer)
> library(car)
> library(reshape)
> 
> #make dataframe
> ID=c("a","b","c","d","e","f","g","h","i","j")
> type=c("type1","type2","type3","type2","type2","type1","type2"
> ,"type1","type1","type3")
> dat_feit_lo=c(13229222400,13510803600,13463193600,13491619200,
> 13502732400,13514315400,13463193600,13514718600,13514497200,13
> 515031800)
> dat_feit_hi=c(13502591940,13510803600,13464798000,13508697600,
> 13514036100,13514315400,13507862400,13514719380,13514432400,13
> 515036600)
> dat_pol=c(13512488400,13510877580,13468415940,13508697600,1351
> 4036100,13514315400,13513528800,13514719380,13514809800,13515037260)
> dat_avv_start=c(13512502320,13510936200,13513705980,1351422744
> 0,13514217300,13514396280,13514636520,13514810580,13514909640,
> 13515099060)
> feiten<-data.frame(ID,type,dat_feit_lo,dat_feit_hi,dat_pol,dat
> _avv_start)
> 
> #make POSIX of date variables
> feiten$dat_feit_lo<-as.POSIXct(feiten$dat_feit_lo,
> origin="1582-10-14",tz="GMT")
> feiten$dat_feit_hi<-as.POSIXct(feiten$dat_feit_hi,
> origin="1582-10-14",tz="GMT")
> feiten$dat_pol<-as.POSIXct(feiten$dat_pol, 
> origin="1582-10-14",tz="GMT")
> feiten$dat_avv_start<-as.POSIXct(feiten$dat_avv_start,
> origin="1582-10-14",tz="GMT")
> 
> #sort & melt data#
> feiten$ID<-with(feiten,reorder(reorder(reorder(ID,1/as.numeric
> (dat_pol)),1/as.numeric(dat_avv_start)),as.numeric(type)))
> sortframe=function(df,...)df[do.call(order,list(...)),]
> data_sort<-with(feiten,sortframe(feiten,as.numeric(type),1/as.
> numeric(dat_avv_start),1/as.numeric(dat_pol)))
> data.melt<-melt.data.frame(data_sort, id=c("ID","type"), 
> variable_name =
> "time")
> levels(data.melt$time)<-c("fact low","fact 
> high","complaint","hearing")
> 
> 
> #make plot#
> data.melt$pos<-data.melt$value>as.POSIXlt("2010-12-01 00:00:00")
> data.melt$pos[is.na(data.melt$pos)]<-'FALSE'
> 
> plot<-
> ggplot(data.melt,aes(value,ID)) +
> geom_point(aes(groups=time,colour=time,shape=time)) +
> facet_grid(type~pos,scales="free",space="free") +
> opts(strip.text.y=theme_text())+
> xlab(NULL) + ylab(NULL)+
> opts(axis.text.x = theme_text(angle = 90, hjust = 1, size = 8)) +
> opts(legend.text = theme_text(hjust=1, size = 8))+
> opts(legend.position="top",legend.direction="horizontal")+
> scale_shape_manual(values = c(1,3,0,2),name="")  +
> scale_colour_manual(values =
> c("red","red","royalblue4","mediumvioletred"),name="")
> 
> 
> ----- Original Message ----- 
> From: "Strategische Analyse CSD Hasselt" <csd.sa op fedpolhasselt.be>
> To: <r-help op R-project.org>
> Sent: Thursday, February 10, 2011 2:40 PM
> Subject: Ggplot: free x-scales in a facet-grid
> 
> 
> > Hello,
> >
> > I have a ggplot that has the looks of the plot that I want, 
> but it doesn't 
> > have the right layout.
> >
> > The data is an ordered melted dataframe:
> > - ID
> > - type (to use for a faced grid)
> > - time - type
> > - time - value (POSIXct)
> > - pos (to use for a faced grid, this is an index to split the plot)
> >
> > The goal of the plot is to create a time line for each ID 
> (different 
> > points of time). The ID's are split in facets according to 
> their type.
> >
> > The plot will look like this (the numbers refer to the ID, 
> the letters to 
> > the time values):
> >
> > 1     x            o                    s                TYPE1
> > 2        x            o                       s
> > 3 x        o                                s            TYPE2
> > 4                x        o                    s         TYPE3
> >
> > The data are ordered within each type, according to date 's'.
> >
> > Now here's the problem. The most data are between the 
> periode 01/12/2010 
> > and 31/01/2011. But there are some outliers, going back to 2003.
> > Now I would like to split the plot in 2 (based on the index 
> 'pos', split 
> > date = 01/12/2010), so the left part of the plot are the 
> time values 
> > before this date (scale_x_datetime major = 1 year), and the 
> right part of 
> > the plot are the time values after this date 
> (scale_x_datetime major=1 
> > day).
> >
> > Hereby also the R-code (simplified):
> > ggplot(data_plot.melt,aes(timevalue,ID)) +
> > geom_point(aes(groups=timetype,colour=timetype,shape=timetype)) +
> > facet_grid(type ~pos,scales="free",space="free") +
> > xlab(NULL) + ylab(NULL)
> >
> > The scales of y has to be free, because the number of ID's per type 
> > differ. The scales of x has to be free, so the scales 
> differ in the left 
> > and right part of the plot.
> > This code succeeds in my goal, but the left part of the 
> plot is very big, 
> > and the right part very very small. However, the most 
> important part of 
> > the plot is the right part. The left part is only to 
> mention the outliers, 
> > to read the plot correctly.
> >
> > I don't know if it's possible to get a plot like I want?
> >
> > Before I added the following code to make the plot, but 
> then I loose the 
> > information of every time value before 01/12/2010:
> > + scale_x_datetime (major = "1 
> > days",limits=c(as.numeric(as.POSIXlt("2010-12-01 
> > 00:00:00")),as.numeric(as.POSIXlt("2011-01-31 22:00:00"))),format = 
> > "%b-%d",expand=c(0,0))
> >
> > Thank you very much in advance!
> >
> > Ann Frederix
> 
> ______________________________________________
> R-help op r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 


More information about the R-help mailing list