[R] ggplot2: connecting medians of boxes using facet_wrap. Changing median marker.
Anna Zakrisson Braeunlich
anna.zakrisson at su.se
Thu Sep 5 17:08:50 CEST 2013
Hi and thank you for the help and for the fast reply!
A. One thing still is a problem. I have prior my first mail tried to not fill the boxes. The result is a different plot.
If chosing to do:
ggplot(mydata, aes(x = week, y = var1))
instead of
ggplot(mydata, aes(x = week, y = var1, fill=organism))
I get the median of all the levels of organisms plotted per week and not the median per organism level per week.
I would like to have white boxes, but to define each level of organism with a different non-filled symbol that mark the median of each box.
B. Can I define the shapes of the geom_point other than the default filled points that I will automatically generate when I define:
geom_point(stat="summary", fun.y = "median", mapping=aes(shape=organism)) + # I need non-filled symbols
The two versions mentioned in A are:
###################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)))
###################VERSION 1####################################
p <- ggplot(mydata, aes(x = week, y = var1)) + #here all organisms are bunted together
geom_boxplot() +
facet_wrap(~ station, ncol = 3) +
theme_bw() +
theme(strip.background = element_blank())+
ylab("var1")+
xlab("week") +
geom_line(stat="summary", fun.y = "median",
mapping=aes(linetype=organism, group=organism))+ #must add jitter if using this
geom_point(stat="summary", fun.y = "median", mapping=aes(shape=organism)) + #must be unfilled
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
###################VERSION 2####################################
p <- ggplot(mydata, aes(x = week, y = var1, fill=organism)) + #here the levels in organism are considered
geom_boxplot() +
facet_wrap(~ station, ncol = 3) +
theme_bw() +
theme(strip.background = element_blank())+
ylab("var1")+
xlab("week") +
geom_line(stat="summary", fun.y = "median",
mapping=aes(linetype=organism, group=organism))+ #must add jitter if using this
geom_point(stat="summary", fun.y = "median", mapping=aes(shape=organism)) + #must be unfilled
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
#############################################################################
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
><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .• `•. .><((((º>`•. . • `•. .• `•. .><((((º>
________________________________________
From: Ista Zahn [istazahn at gmail.com]
Sent: 05 September 2013 16:02
To: Anna Zakrisson Braeunlich
Cc: r-help at r-project.org
Subject: Re: [R] ggplot2: connecting medians of boxes using facet_wrap. Changing median marker.
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