[R] ggplot2: changing strip text in facet_grid and a legend text problem

Ista Zahn istazahn at gmail.com
Wed Sep 18 15:52:44 CEST 2013


Hi Anna,

See in line below.

On Wed, Sep 18, 2013 at 7:29 AM, Anna Zakrisson Braeunlich
<anna.zakrisson at su.se> wrote:
> Hi,
>
> Dummy data script and scripts are attached below.
>
> I would like to change the plot to look like this:
> https://www.google.se/search?q=facet_grid&bav=on.2,or.r_qf.&bvm=bv.52288139,d.Yms&biw=1454&bih=704&dpr=1&pdl=300&um=1&ie=UTF-8&hl=sv&tbm=isch&source=og&sa=N&tab=wi&ei=vY05Uu3vD8aHtAawsYDIBw#facrc=_&imgdii=_&imgrc=IyGjjRic2YJAsM%3A%3BzWmfhhY7LA1p2M%3Bhttps%253A%252F%252Fgithub-camo.global.ssl.fastly.net%252Fc18764797ada04cb8006a2bdaa962c65bfd9bf09%252F687474703a2f2f6661726d362e737461746963666c69636b722e636f6d2f353333332f373036363335373537335f336537313031326534655f6f2e706e67%3Bhttps%253A%252F%252Fgithub.com%252Fcboettig%252Fwrightscape%252Fblob%252Fmaster%252Finst%252Fexamples%252Flabrid_par_bootstrap.md%3B576%3B360
>
> i.e. I want the panes labelled on top and on the right side of the graph. I want however, the text horizontally on the right side.
>
> I do not understand why facet_grid plotted it this way for me. Why?

Because you did not use facet_grid. Change

  facet_wrap(year~ station) +

to

  facet_grid(year~ station) +

>
> I would also like to change the text in the legend of this plot.
> I have tried several solutions including:
>
> scale_fill_discrete(guide = guide_legend(), labels=c("cyanobacteria", "zooplankton"))
> scale_fill_discrete(name="organism",
>                         breaks=c("cyano", "seston"),
>                         labels=c("cyanobacteria", "seston"))
>
> for some reason nothing works. Ideas?

It doesn't work because you have not mapped anything to fill.
Presumably you want

    scale_shape_discrete(name="organism",
                        breaks=c("cyano", "seston"),
                        labels=c("cyanobacteria", "seston"),
                         solid=FALSE) +
    scale_linetype_discrete(name="organism",
                        breaks=c("cyano", "seston"),
                        labels=c("cyanobacteria", "seston"))

Best,
Ista

>
> mydata <- data.frame(
>   D15N = c(runif(100, min = -2), runif(100), runif(100, max = 2), runif(100)),
>   year = rep(c('2007', '2008'), each = 100),
>   organism = rep(c('cyano', 'seston'), each = 200),
>   station = sample(c('B1', 'H2', 'H3', 'H4'), 400, replace = TRUE),
>   week = sample(c('19', '21', '23', '25'), 400, replace = TRUE))
>
>
> names(mydata)
> head(mydata)
> str(mydata)
>
>
> Summdata <- ddply(mydata, .(week, organism, year, station), summarise, mean = mean(D15N),
>                        sd = sd(D15N))
>
> p <- ggplot(mydata, aes(x = week, y = D15N)) +
>   (mapping=aes(group=interaction(week, organism))) +
>   facet_wrap(year~ station) +
>   theme_bw() +
>   geom_errorbar(data = Summdata, aes(ymin = mean - sd, y = mean,
>                                           ymax = mean + sd),
>                 size = 1, width = 0.05, color = "black")+
>   theme(strip.background = element_blank())+
>   ylab(expression(paste("",delta^{15}, "N")))+
>   xlab("Week") +
>   geom_line(stat="summary", fun.y = "mean",
>             mapping=aes(linetype=organism, group=organism))+ #must add jitter if using this
>   geom_point(stat="summary", fun.y = "mean", size=2, mapping=aes(shape=organism))+
>   scale_shape(solid = FALSE) + #must be unfilled
>   geom_hline(yintercept=0, linetype=3) + #draws dotted line at 0
>   theme(strip.text.x = element_text(size = 16, colour="black", family="serif", angle=00)) +
>   theme(strip.text.y = element_text(size = 16, colour="black", family="serif", angle=00)) +
>   theme(axis.text.x = element_text(size = 16, colour="black", family="serif", angle=00)) +
>   theme(axis.text.y = element_text(size = 16, colour="black", family="serif", angle=00)) +
>   theme(axis.title.x = element_text(size=16, colour="black", family="serif", angle=00))+
>   theme(axis.title.y = element_text(size=16, colour="black", family="serif", angle=90)) +
>   scale_fill_discrete(guide = guide_legend(), labels=c("cyanobacteria", "zooplankton"))
>
> p
>
> with kind regards
>
>
> Anna Zakrisson Braeunlich
> PhD student
>
> Department of Ecology, Environment and Plant Sciences
> Stockholm University
> Svante Arrheniusv. 21A
> SE-106 91 Stockholm
> Sweden/Sverige
>
> Lives in Berlin.
> For paper mail:
> Katzbachstr. 21
> D-10965, Berlin - Kreuzberg
> Germany/Deutschland
>
> E-mail: anna.zakrisson at su.se
> Tel work: +49-(0)3091541281
> Mobile: +49-(0)15777374888
> LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b
>
>><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .• `•. .><((((º>`•. . • `•. .• `•. .><((((º>
>
>         [[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