[R] is there a way to avoid "traveling" grid?

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Oct 7 14:36:14 CEST 2010


Dimitri,

1. The grid will always be on the background. You can use geom_vline() and geom_hline() to draw horizontal and vertical lines at the positions that you want them. 

2. Note that you must 'stack' the data yourself. Geom_area() just plots a polygon with 0 as one of the boundaries. When you have multiple categories, then position = "identity" will plot overlapping polygons. The order of the polygons depends on the order of the factor. The first level will be drawn first, then the second,... So the polygon of the last level will be on top. So you must figure out in what order you need the levels.

3. If you want specific colour you will need scale_fill_manual. You don't have to use a named value, it that case the colours will be assigned in the same order as the levels of your factor. I'm not sure how you would what to automate that? If you don't want specific colours then have a look a scale_fill_discrete or scale_fill_brewer(). More details on http://had.co.nz/ggplot2/

HTH,

Thierry

----------------------------------------------------------------------------
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: Dimitri Liakhovitski [mailto:dimitri.liakhovitski op gmail.com] 
> Verzonden: dinsdag 5 oktober 2010 17:49
> Aan: ONKELINX, Thierry
> CC: r-help op r-project.org
> Onderwerp: Re: [R] is there a way to avoid "traveling" grid?
> 
> Thierry,
> your ggplot solution really looks great and very parsimonious!
> May I ask a couple of questions?
> 1. Is there a way to make the grid appear on top of the 
> colored areas instead of under them - like I did in the plot solution?
> 2. In your code line Molten2$variable <- 
> factor(Molten2$variable, levels = c("z", "y", "x","a")) - how 
> did you chose the order of the levels? It's important for me 
> to know because I'll have to deal with a lot of variables and 
> would like to automate this part (you might have noticed I 
> want the variable with the smallest negatives to be first at 
> the bottom of the chart and the variable with the smallest 
> positive to be at the top).
> 3. In your scale_fill_manual line of code - looks like the 
> order is manual again. Is there a way to define the colors, 
> so to say "automatically" - in accordance to the areas of the chart?
> 
> Thanks a lot!
> 
> 
> On Tue, Oct 5, 2010 at 11:28 AM, ONKELINX, Thierry
> <Thierry.ONKELINX op inbo.be> wrote:
> > Dear Dimitri,
> >
> > Ggplot2 solves your problem with the gridlines and requires 
> much less 
> > code. You only need to reshape your data somewhat.
> >
> > library(ggplot2)
> > #changing the dataset
> > my.data2 <- my.data
> > my.data2$x <- my.data$x + my.data$a
> > my.data2$z <- my.data$y + my.data$z
> > Molten2 <- melt(my.data2, id.vars = "date") Molten2$variable <- 
> > factor(Molten2$variable, levels = c("z", "y", "x",
> > "a"))
> >
> > #basic plot
> > ggplot(Molten2, aes(x = date, y = value, fill = variable)) +
> >        geom_area(position = "identity")
> >
> > #changing the lay-out a bit
> > ggplot(Molten2, aes(x = date, y = value, fill = variable)) +
> >        geom_area(position = "identity") +
> >        scale_fill_manual(value = c(z = "orange", y = "green", x = 
> > "blue", a = "yellow")) +
> >        scale_x_date(major = "months", format="%d-%m-%Y") +
> >        opts(axis.text.x = theme_text(angle = 90, hjust = 1))
> >
> > HTH,
> >
> > Thierry
> >
> > 
> ----------------------------------------------------------------------
> > --
> > ----
> > 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 Dimitri Liakhovitski
> >> Verzonden: dinsdag 5 oktober 2010 16:37
> >> Aan: r-help op r-project.org
> >> Onderwerp: [R] is there a way to avoid "traveling" grid?
> >>
> >> Hello!
> >>
> >> If you run the whole code below, it'll produce a stacked 
> diagram. And 
> >> it looks good - because the tick-marks are aligned with the grid.
> >> However, if I stretch the graph window, grid becomes 
> misaligned with 
> >> the tickmarks. Or, rather, it seems aligned for the first and the 
> >> last tick mark, but not for tickmarks in between.
> >> Can it be addressed?
> >> Thank you!
> >>
> >> Dimitri
> >>
> >>
> >> ### Creating a data set with both positives and negatives
> >> my.data<-data.frame(date=c(20080301,20080401,20080501,20080601
> > 
> ,20080701,20080801,20080901,20081001,20081101,20081201,20090101,200902
> > 01
> > 
> ,20090301,20090401,20090501,20090601,20090701,20090801,20090901,200910
> > 01 ,20091101,20091201,> 
> 20100101,20100201,20100301,20100402,20100503),
> >> x=c(1.1, 1, 1.6, 1, 2, 1.5, 2.1, 1.3, 1.9, 1.1, 1, 1.6, 1, 2, 1.5, 
> >> 2.1, 1.3, 1.9, 1.1, 1, 1.6, 1, 2, 1.5, 2.1, 1.3, 1.9),
> >> y=c(-4,-3,-6,-5,-7,-5.2,-6,-4,-4.9,-4,-3,-6,-5,-7,-5.2,-6,-4,-
> >> 4.9,-4,-3,-6,-5,-7,-5.2,-6,-4,-4.9),
> >> z=c(-0.2,-0.3,-0.4,-0.1,-0.2,-0.05,-0.2,-0.15,-0.06,-0.2,-0.3,
> >> -0.4,-0.1,-0.2,-0.05,-0.2,-0.15,-0.06,-0.06,-0.2,-0.3,-0.4,-0.
> >> 1,-0.2,-0.05,-0.2,-0.15),
> >> a=c(10,13,15,15,16,17,15,16,14,10,13,15,15,16,17,15,16,14,10,1
> >> 3,15,15,16,17,15,16,14))
> >> my.data$date<-as.character(my.data$date)
> >> my.data$date<-as.Date(my.data$date,"%Y%m%d")
> >> (my.data)
> >> str(my.data)
> >>
> >> positives<-which(colSums(my.data[2:ncol(my.data)])>0) # which vars 
> >> have positive column sums?
> >> negatives<-which(colSums(my.data[2:ncol(my.data)])<0) # which vars 
> >> have negative column sums?
> >>
> >> y.max<-1.1*max(rowSums(my.data[names(positives)])) # the 
> max on the y 
> >> axis of the chart
> >> y.min<-1.1*min(rowSums(my.data[names(negatives)])) # the 
> min on the y 
> >> axis of the chart ylim <- c(y.min, y.max)
> >> order.positives<-rev(rank(positives))
> >> order.of.pos.vars<-names(order.positives)
> >> order.negatives<-rev(rank(negatives))
> >> order.of.neg.vars<-names(order.negatives)
> >> order<-c(order.negatives,order.positives)
> >> order.of.vars<-names(order)   # the order of variables on 
> the chart - 
> >> from the bottom up ### so, the bottom-most area should be 
> for z, the 
> >> second from the bottom area- for y (above z)
> >>
> >> all.colors<-c('red','blue','green','orange','yellow','purple')
> >> xx <- c(my.data$date, rev(my.data$date))
> >> bottom.y.coordinates<-rowSums(my.data[names(negatives)])
> >>
> >>
> >> par(mar=c(5,4,4,6),xpd=F)
> >> plot(x=my.data$date, y=bottom.y.coordinates, ylim=ylim, 
> col='white', 
> >> type='l', xaxt='n',
> >>       ylab='Title for Y', xlab="", main='Chart Title')
> >>
> >> for(var in order.of.neg.vars){
> >>       top.line.coords<-bottom.y.coordinates-my.data[[var]]
> >>       bottom.coords<-c(bottom.y.coordinates,rev(top.line.coords))
> >>
> >> polygon(xx,bottom.coords,col=all.colors[which(names(my.data)
> >> %in% var)])
> >>       bottom.y.coordinates<-top.line.coords
> >> }
> >>
> >> for(var in order.of.pos.vars){
> >>       top.line.coords<-bottom.y.coordinates+my.data[[var]]
> >>       bottom.coords<-c(bottom.y.coordinates,rev(top.line.coords))
> >>
> >> polygon(xx,bottom.coords,col=all.colors[which(names(my.data)
> >> %in% var)])
> >>       bottom.y.coordinates<-top.line.coords
> >> }
> >>
> >> axis(1, labels =format(as.Date(my.data$date, origin="1970-01-01"), 
> >> "%Y-%m-%d"), at=my.data$date, las=2,cex.axis=0.7) 
> >> grid(nx=(length(my.data$date)-1),ny=NULL,col = "lightgray", lty = 
> >> "dotted",lwd = par("lwd"))
> >>
> >> legend(par()$usr[2],
> >>   mean(par()$usr[3:4]),
> >>   c("Blue","Yellow","Green","Orange"),
> >>   xpd=T,
> >>   bty="n",
> >>   pch=12,
> >>   col=c("Blue","Yellow","Green","Orange"))
> >>
> >> ______________________________________________
> >> 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.
> >>
> >
> > Druk dit bericht a.u.b. niet onnodig af.
> > Please do not print this message unnecessarily.
> >
> > Dit bericht en eventuele bijlagen geven enkel de visie van de 
> > schrijver weer en binden het INBO onder geen enkel beding, 
> zolang dit 
> > bericht niet bevestigd is door een geldig ondertekend document. The 
> > views expressed in  this message and any annex are purely 
> those of the 
> > writer and may not be regarded as stating an official position of 
> > INBO, as long as the message is not confirmed by a duly 
> signed document.
> >
> 
> 
> 
> --
> Dimitri Liakhovitski
> Ninah Consulting
> www.ninah.com
> 

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.



More information about the R-help mailing list