[R] ggplot2: connecting medians of boxes using facet_wrap. Changing median marker.

Ista Zahn istazahn at gmail.com
Thu Sep 5 16:02:59 CEST 2013


Hi Anna,

On Thu, Sep 5, 2013 at 6:13 AM, Anna Zakrisson Braeunlich
<anna.zakrisson at su.se> wrote:
> # Dear all,
>
> # Thank you for taking your time.
>
> # What I would like to do:
>
> # (Run the code below to see what I am referring to)
>
> # I want lines connecting the medians of of the boxes. I do not want a function, just a simple,
> # straight connection between points. I also would like the lines to be different: lty=c(1:3)

geom_line(stat="summary", fun.y = "median",
mapping=aes(linetype=organism, group=organism))

>
> # Furthermore, I wish to change the line/dot marking the medians to be pch=c(1:3). It seems not to be so simple when using facet_wrap? I have obviously missed something obvious.

geom_point(stat="summary", fun.y = "median", mapping=aes(shape=organism)) +

>
> # Finally, I would like the boxes to be filled white and to have the legend reflecting this.

Then don't tell ggplot to color them. Change

ggplot(mydata, aes(x = week, y = var1, fill = organism))

to

ggplot(mydata, aes(x = week, y = var1))
>
> # I know that this was many questions, I apologize if some may seem basic. It is just that I am
> # "jumping packages" the whole time and sometimes par() adjustments work and sometimes not.
> # I have searched alot for answers, but as a ggplot2 beginner, I have failed to find a solution
> # to my problems above.


Hope this helps!

Best,
Ista

>
> # I would like to thank the programmers for a great package. The layer principle is much easier to work with.
>
> ####
>
> # Some dummy data:
> mydata<- data.frame(week = factor(rep(c("19", "21", "23", "25", "27", "29", "31", "33",
>                                            "35", "37", "39"), each = 45*3)), #week
>                     station = factor(rep(c("BY31", "B1", "H2", "H3", "H4",
>                                            "H5", "H6", "H7", "H8"), each = 15)), #station
>                     organism = factor(rep(c("zpl", "ses", "cy"), each = 5)), #organism
>                     var1 = rnorm(1485, mean = rep(c(0, 3, 15), each = 40),
>                                  sd = rep(c(1, 3, 6), each = 20)))
>
> p <- ggplot(mydata, aes(x = week, y = var1, fill = organism)) +
>   geom_boxplot() +
>   facet_wrap(~ station, ncol = 3) +
>   theme_bw() +
>   theme(strip.background = element_blank())+
>   ylab("var1")+
>   xlab("week") +
>   geom_smooth(aes(group = 1), method="lm", se = F) +  #Here is my problem.
>   theme(strip.text.x = element_text(size = 12, colour="black", family="serif", angle=00)) +
>   theme(axis.text.x = element_text(size = 12, colour="black", family="serif", angle=90)) +
>   theme(axis.text.y = element_text(size = 12, colour="black", family="serif", angle=00)) +
>   geom_hline(yintercept=0, linetype=3) #draws dotted line at 0
>
> p
>
>   # method="lm" is definately wrong,
>   # I just added it to be a ble to draw some lines at all.
>   # I also suspect geom_smooth to be wrong.
> ### TO CLARIFY: I want the medians connected within each level. cy medians connected and
>   # and ses medians connected and zpl connected. Not cy-ses-zpl, but that is perhaps obvious.
> ### Thank you for your time!
>   # with kind regards
>   # A. Zakrisson
>
>
>
> 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