[R] help to polish plot in ggplot2
baptiste auguie
baptiste.auguie at googlemail.com
Thu Aug 12 09:47:57 CEST 2010
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.auguie at 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 <mahbub72 at 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 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.
>>
>
More information about the R-help
mailing list