Hello r-help. I have been working on making a graph and have several solutions but they are tedious at best. Here is an example dataset: catg<-(c(1,2,3,2,4,3,2,1,4,3,1)) min<-(c(1,2,3,3,4,5,6,6,3,2,1)) max <-(c(10,6,8,6,7,3,10,9,10,8,9)) cbind(min,max,catg) What I want to create is a basically a bar chart, but I define the min (min) and max (max) for the bars. These bars are separated into three categories (catg). Within each category the bars are allowed to overlap. I set the bars to have a transparent color so that I can see agreement of the items within a category based on the darkness of color. One method I came up with, is to use ggplot2 and geom_tile but then I have to give x,y coordinates for every corner of every box. Considering the size of my real datasets, and not this little example dataset, this seems annoying. I could even just graph lines with the defined min and max. With that I could make the lines thick and achieve the effect I am looking for. Hopefully I have missed an obvious method to graph this. Thanks, Daisy -- Daisy Englert Duursma Room E8C156 Dept. Biological Sciences Macquarie University NSW 2109 Australia
On Sun, Jun 6, 2010 at 11:53 PM, Daisy Englert Duursma <daisy.duursma at gmail.com> wrote:> Hello r-help. > > I have been working on making a graph and have several solutions but > they are tedious at best. > > Here is an example dataset: > > catg<-(c(1,2,3,2,4,3,2,1,4,3,1)) > min<-(c(1,2,3,3,4,5,6,6,3,2,1)) > max <-(c(10,6,8,6,7,3,10,9,10,8,9)) > cbind(min,max,catg) > > What I want to create is a basically a bar chart, ?but I define the > min (min) and max (max) for the bars. > These bars are separated into three categories (catg). Within each > category the bars are allowed to overlap. I set the bars to have a > transparent color so that I can see agreement of the ?items within a > category based on the darkness of color. > > One method I came up with, is to use ggplot2 and geom_tile but then I > have to give x,y coordinates for every corner of every box. > Considering the size of my real datasets, and not this little example > dataset, this seems annoying. > > I could even just graph lines with the defined min and max. With that > I could make the lines thick and achieve the effect I am looking for. > > Hopefully I have missed an obvious method to graph this.One option to look at is segplot() in the latticeExtra package. -Deepayan
On 06/07/2010 04:53 PM, Daisy Englert Duursma wrote:> Hello r-help. > > I have been working on making a graph and have several solutions but > they are tedious at best. > > Here is an example dataset: > > catg<-(c(1,2,3,2,4,3,2,1,4,3,1)) > min<-(c(1,2,3,3,4,5,6,6,3,2,1)) > max<-(c(10,6,8,6,7,3,10,9,10,8,9)) > cbind(min,max,catg) > > What I want to create is a basically a bar chart, but I define the > min (min) and max (max) for the bars. > These bars are separated into three categories (catg). Within each > category the bars are allowed to overlap. I set the bars to have a > transparent color so that I can see agreement of the items within a > category based on the darkness of color. > > One method I came up with, is to use ggplot2 and geom_tile but then I > have to give x,y coordinates for every corner of every box. > Considering the size of my real datasets, and not this little example > dataset, this seems annoying. > > I could even just graph lines with the defined min and max. With that > I could make the lines thick and achieve the effect I am looking for. >Hi Daisy, How about this? offset<-c(-0.1,-0.1,-0.1,0,-0.1,0,0.1,0,0,0.1,0.1) plot(0,xlim=c(0.5,4.5),ylim=c(0,10),type="n") segments(catg+offset,min,catg+offset,max,lwd=5,col=3) You can program a function to calculate the offsets if you want to do lots of these. Jim
Dear Daisy, This is easy woth ggplot2. Use geom_crossbar() and set the middle bar at the minimum of maximum. library(ggplot2) dataset <- data.frame(catg = (c(1,2,3,2,4,3,2,1,4,3,1)), min = (c(1,2,3,3,4,5,6,6,3,2,1)), max = (c(10,6,8,6,7,3,10,9,10,8,9))) ggplot(dataset, aes(x = catg, y = min, ymin = min, ymax = max)) + geom_crossbar(fill = "red", alpha = 0.2, fatten = 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 at 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 at r-project.org > [mailto:r-help-bounces at r-project.org] Namens Jim Lemon > Verzonden: maandag 7 juni 2010 13:27 > Aan: Daisy Englert Duursma > CC: r-help at r-project.org > Onderwerp: Re: [R] barchart but with boxes > > On 06/07/2010 04:53 PM, Daisy Englert Duursma wrote: > > Hello r-help. > > > > I have been working on making a graph and have several > solutions but > > they are tedious at best. > > > > Here is an example dataset: > > > > catg<-(c(1,2,3,2,4,3,2,1,4,3,1)) > > min<-(c(1,2,3,3,4,5,6,6,3,2,1)) > > max<-(c(10,6,8,6,7,3,10,9,10,8,9)) > > cbind(min,max,catg) > > > > What I want to create is a basically a bar chart, but I define the > > min (min) and max (max) for the bars. > > These bars are separated into three categories (catg). Within each > > category the bars are allowed to overlap. I set the bars to have a > > transparent color so that I can see agreement of the items > within a > > category based on the darkness of color. > > > > One method I came up with, is to use ggplot2 and geom_tile > but then I > > have to give x,y coordinates for every corner of every box. > > Considering the size of my real datasets, and not this > little example > > dataset, this seems annoying. > > > > I could even just graph lines with the defined min and max. > With that > > I could make the lines thick and achieve the effect I am > looking for. > > > Hi Daisy, > How about this? > > offset<-c(-0.1,-0.1,-0.1,0,-0.1,0,0.1,0,0,0.1,0.1) > plot(0,xlim=c(0.5,4.5),ylim=c(0,10),type="n") > segments(catg+offset,min,catg+offset,max,lwd=5,col=3) > > You can program a function to calculate the offsets if you > want to do lots of these. > > Jim > > ______________________________________________ > R-help at 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.
Thanks to all, I did eventually come up with a solution which involved sub-setting the data first into categories, making an empty plot, and then plotting rectangles. I really like the ggplot2 and I will use that next time. My ugly script is below Thanks, Daisy plot(1, xlim=c(5,45),ylim=c(0,10),xlab="Year",ylab="+/- 1 \u03c3 Mean Patch Size (ha)",axes=F,type="n") title(main = list("Patch Size Agreement", cex=1.4)) axis(2) box() mtext(c(2010,2030,2050,2070),side=1,line = 1, at=c(10,20,30,40)) position<-seq(10, 40, length.out=4 ) xlef<-position-2.5 xrig <-position+2.5 for(i in 1:nrow(sub00)){ min_y<-sub00$minsd_ha[i] max_y<-sub00$maxsd_ha[i] xleft<-xlef[1] xrigh<-xrig[1] rect(xleft, min_y, xrigh, max_y,border = NA,col=rgb(0, 0, 0, alpha=0.03)) } for(i in 1:nrow(sub01)){ min_y<-sub01$minsd_ha[i] max_y<-sub01$maxsd_ha[i] xleft<-xlef[2] xrigh<-xrig[2] rect(xleft, min_y, xrigh, max_y,border = NA,col=rgb(0, 0, 0, alpha=0.03)) } for(i in 1:nrow(sub02)){ min_y<-sub02$minsd_ha[i] max_y<-sub02$maxsd_ha[i] xleft<-xlef[3] xrigh<-xrig[3] rect(xleft, min_y, xrigh, max_y,border = NA,col=rgb(0, 0, 0, alpha=0.03)) } for(i in 1:nrow(sub03)){ min_y<-sub03$minsd_ha[i] max_y<-sub03$maxsd_ha[i] xleft<-xlef[4] xrigh<-xrig[4] rect(xleft, min_y, xrigh, max_y,border = NA,col=rgb(0, 1, 0, alpha=0.03)) }