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

Strategische Analyse CSD Hasselt csd.sa at fedpolhasselt.be
Thu Feb 10 14:40:34 CET 2011


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