Re: [R] help to polish plot in ggplot2
To illustrate the second option I proposed, library(ggplot2) library(gridExtra) category - paste(Geographical Category, 1:10) grp1 - rnorm(10, mean=10, sd=10) grp2 - rnorm(10, mean=20, sd=10) grp3 - rnorm(10, mean=15, sd=10) grp4 - rnorm(10, mean=12, sd=10) mydat - data.frame(category,grp1,grp2,grp3,grp4) dat.m - melt(mydat) p - qplot(1,value, data=dat.m, geom=bar, xlab=,ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) + coord_flip()+ facet_grid(category ~ ., space=free)+scale_x_discrete(breaks=c(2,4))+opts(strip.text.y = theme_text(hjust = 0)) labs - llply(category, textGrob) ## add a blank grob below as the y title is aligned with the full ggplot2 height, ## not just the panel my.labels - do.call(arrangeGrob, c(labs, list(ncol=1, left=My y title, sub= ))) # grid.draw(my.labels) ## hack: define the width of my.labels ylab - gTree(children=gList(my.labels, rectGrob()), cl=mylabels ) widthDetails.mylabels - function(x) max(stringWidth(category)) + unit(1, line) ## hack: tweak ggplot2's axis.title.y option to use our gTree foo - function() function(label, x, y) ylab p + opts(strip.text.y =theme_blank(), strip.background=theme_blank()) + opts( axis.title.y = foo()) HTH, baptiste On 12 August 2010 07:44, baptiste auguie baptiste.aug...@googlemail.com wrote: Hi, One way you could do it is to create a separate graph for each category. The y axis labels would replace the strip labels. You could then stack the graphs on the page, and add a common legend. The tricky part would be to make sure the different panels have the same width and height. Another option might be to hack a custom Grob (gTree) for the y-axis title so that it would draw the current y-axis title and also the labels for the facets next to it. Of course you'd also get rid of the strips in this case. Best, baptiste On 11 August 2010 15:39, Mahbubul Majumder mahbu...@gmail.com wrote: Hi, I wanted to generate a plot which is almost like the plot generated by the following codes. category - paste(Geographical Category, 1:10) grp1 - rnorm(10, mean=10, sd=10) grp2 - rnorm(10, mean=20, sd=10) grp3 - rnorm(10, mean=15, sd=10) grp4 - rnorm(10, mean=12, sd=10) mydat - data.frame(category,grp1,grp2,grp3,grp4) dat.m - melt(mydat) p - qplot(1,value, data=dat.m, geom=bar, xlab=,ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) p + coord_flip()+ facet_grid(category ~ ., space=free)+scale_x_discrete(breaks=c(2,4))+opts(strip.text.y = theme_text(hjust = 0)) Now the only modification I need from this plot is that I want the grid labels (text) on the left hand side with right justification and white background. My prospective plot should have labels like the the plot generated by the codes below. The reason why I don't like the plot below is that it does not show separate grid for each category. p - qplot(category,value, data=dat.m, geom=bar, ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) p + coord_flip() Can you help me generate my vision plot? -- Mahbub Majumder Graduate Student Dept. of Statistics Iowa State University [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] help to polish plot in ggplot2
baptiste, I have two more questions. How can I get the category labels right justified? It seems that I need to change the size of the text too. Since in my real data the text size are big and they appears to be even bigger than my plot area. So, my second question is how can I change the text size? Thanks again for your help. On Thu, Aug 12, 2010 at 8:02 AM, baptiste Auguié baptiste.aug...@googlemail.com wrote: you just need to remove the rectGrob() from ylab, as in ylab - gTree(children=gList(my.labels), cl=mylabels ) baptiste On Aug 12, 2010, at 2:59 PM, Mahbubul Majumder wrote: baptiste, This is exactly what I wanted. Many thanks for this. I have one problem though. How can I get rid of the boundary box of y axis title? On Thu, Aug 12, 2010 at 2:47 AM, baptiste auguie baptiste.aug...@googlemail.com wrote: To illustrate the second option I proposed, library(ggplot2) library(gridExtra) category - paste(Geographical Category, 1:10) grp1 - rnorm(10, mean=10, sd=10) grp2 - rnorm(10, mean=20, sd=10) grp3 - rnorm(10, mean=15, sd=10) grp4 - rnorm(10, mean=12, sd=10) mydat - data.frame(category,grp1,grp2,grp3,grp4) dat.m - melt(mydat) p - qplot(1,value, data=dat.m, geom=bar, xlab=,ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) + coord_flip()+ facet_grid(category ~ ., space=free)+scale_x_discrete(breaks=c(2,4))+opts(strip.text.y = theme_text(hjust = 0)) labs - llply(category, textGrob) ## add a blank grob below as the y title is aligned with the full ggplot2 height, ## not just the panel my.labels - do.call(arrangeGrob, c(labs, list(ncol=1, left=My y title, sub= ))) # grid.draw(my.labels) ## hack: define the width of my.labels ylab - gTree(children=gList(my.labels, rectGrob()), cl=mylabels ) widthDetails.mylabels - function(x) max(stringWidth(category)) + unit(1, line) ## hack: tweak ggplot2's axis.title.y option to use our gTree foo - function() function(label, x, y) ylab p + opts(strip.text.y =theme_blank(), strip.background=theme_blank()) + opts( axis.title.y = foo()) HTH, baptiste On 12 August 2010 07:44, baptiste auguie baptiste.aug...@googlemail.com wrote: Hi, One way you could do it is to create a separate graph for each category. The y axis labels would replace the strip labels. You could then stack the graphs on the page, and add a common legend. The tricky part would be to make sure the different panels have the same width and height. Another option might be to hack a custom Grob (gTree) for the y-axis title so that it would draw the current y-axis title and also the labels for the facets next to it. Of course you'd also get rid of the strips in this case. Best, baptiste On 11 August 2010 15:39, Mahbubul Majumder mahbu...@gmail.com wrote: Hi, I wanted to generate a plot which is almost like the plot generated by the following codes. category - paste(Geographical Category, 1:10) grp1 - rnorm(10, mean=10, sd=10) grp2 - rnorm(10, mean=20, sd=10) grp3 - rnorm(10, mean=15, sd=10) grp4 - rnorm(10, mean=12, sd=10) mydat - data.frame(category,grp1,grp2,grp3,grp4) dat.m - melt(mydat) p - qplot(1,value, data=dat.m, geom=bar, xlab=,ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) p + coord_flip()+ facet_grid(category ~ ., space=free)+scale_x_discrete(breaks=c(2,4))+opts(strip.text.y = theme_text(hjust = 0)) Now the only modification I need from this plot is that I want the grid labels (text) on the left hand side with right justification and white background. My prospective plot should have labels like the the plot generated by the codes below. The reason why I don't like the plot below is that it does not show separate grid for each category. p - qplot(category,value, data=dat.m, geom=bar, ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) p + coord_flip() Can you help me generate my vision plot? -- Mahbub Majumder Graduate Student Dept. of Statistics Iowa State University [[alternative HTML version deleted]] __ R-help@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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Mahbub Majumder Graduate Student Dept. of Statistics Iowa State University -- Mahbub Majumder Graduate Student Dept. of Statistics Iowa State University [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help
[R] help to polish plot in ggplot2
Hi, I wanted to generate a plot which is almost like the plot generated by the following codes. category - paste(Geographical Category, 1:10) grp1 - rnorm(10, mean=10, sd=10) grp2 - rnorm(10, mean=20, sd=10) grp3 - rnorm(10, mean=15, sd=10) grp4 - rnorm(10, mean=12, sd=10) mydat - data.frame(category,grp1,grp2,grp3,grp4) dat.m - melt(mydat) p - qplot(1,value, data=dat.m, geom=bar, xlab=,ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) p + coord_flip()+ facet_grid(category ~ ., space=free)+scale_x_discrete(breaks=c(2,4))+opts(strip.text.y = theme_text(hjust = 0)) Now the only modification I need from this plot is that I want the grid labels (text) on the left hand side with right justification and white background. My prospective plot should have labels like the the plot generated by the codes below. The reason why I don't like the plot below is that it does not show separate grid for each category. p - qplot(category,value, data=dat.m, geom=bar, ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) p + coord_flip() Can you help me generate my vision plot? -- Mahbub Majumder Graduate Student Dept. of Statistics Iowa State University [[alternative HTML version deleted]] __ R-help@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.
Re: [R] help to polish plot in ggplot2
Hi, One way you could do it is to create a separate graph for each category. The y axis labels would replace the strip labels. You could then stack the graphs on the page, and add a common legend. The tricky part would be to make sure the different panels have the same width and height. Another option might be to hack a custom Grob (gTree) for the y-axis title so that it would draw the current y-axis title and also the labels for the facets next to it. Of course you'd also get rid of the strips in this case. Best, baptiste On 11 August 2010 15:39, Mahbubul Majumder mahbu...@gmail.com wrote: Hi, I wanted to generate a plot which is almost like the plot generated by the following codes. category - paste(Geographical Category, 1:10) grp1 - rnorm(10, mean=10, sd=10) grp2 - rnorm(10, mean=20, sd=10) grp3 - rnorm(10, mean=15, sd=10) grp4 - rnorm(10, mean=12, sd=10) mydat - data.frame(category,grp1,grp2,grp3,grp4) dat.m - melt(mydat) p - qplot(1,value, data=dat.m, geom=bar, xlab=,ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) p + coord_flip()+ facet_grid(category ~ ., space=free)+scale_x_discrete(breaks=c(2,4))+opts(strip.text.y = theme_text(hjust = 0)) Now the only modification I need from this plot is that I want the grid labels (text) on the left hand side with right justification and white background. My prospective plot should have labels like the the plot generated by the codes below. The reason why I don't like the plot below is that it does not show separate grid for each category. p - qplot(category,value, data=dat.m, geom=bar, ylab=Percentage of eco change,stat=identity,fill=variable, position=dodge) p + coord_flip() Can you help me generate my vision plot? -- Mahbub Majumder Graduate Student Dept. of Statistics Iowa State University [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.