[R] Ggplot: free x-scales in a facet-grid
Strategische Analyse CSD Hasselt
csd.sa at fedpolhasselt.be
Fri Feb 11 10:08:37 CET 2011
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,13515031800)
dat_feit_hi=c(13502591940,13510803600,13464798000,13508697600,13514036100,13514315400,13507862400,13514719380,13514432400,13515036600)
dat_pol=c(13512488400,13510877580,13468415940,13508697600,13514036100,13514315400,13513528800,13514719380,13514809800,13515037260)
dat_avv_start=c(13512502320,13510936200,13513705980,13514227440,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 at fedpolhasselt.be>
To: <r-help at 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
More information about the R-help
mailing list