Anna Zakrisson Braeunlich
2014-Jan-15 08:42 UTC
[R] ggplot2: how to add new names to legend
Dear all,
I have problems adding new names to the legend (not the legend title).
I have tried all sorts of versions involving scale_fill_discrete and themes.
Weirdly, my method works fine when changing legend when making a barplot
instead.
I have searched and tried and am soon throwing the computer out of the window.
Please, help. I have read the help pages, but I don't understand why this
won't work.
The plot produced below is extremely ugly. Ignore this. I just wanted to add the
full script for my own data. With my own data the plot looks fine.
# 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
year = factor(rep(c("2007", "2008",
"2009"), each = 45)), #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)))
library(ggplot2)
p <- ggplot(mydata,aes(week,var1))+
facet_grid(year~station, scales = "free")+
scale_shape_manual(values = 1:3) +
geom_point(mapping=aes(shape=organism))+
theme_bw() +
theme(strip.background = element_blank()) +
xlab("Week")+
ylab(expression(paste("Biomass (", mu, "g ", L^-1,
")")))+
geom_line(aes(group=organism, linetype=organism))+
theme(strip.text.x = element_text(size = 10, colour="black",
family="serif", angle=00)) +
theme(strip.text.y = element_text(size = 10, colour="black",
family="serif", angle=90)) +
theme(axis.text.x = element_text(size = 10, colour="black",
family="serif", angle=00)) +
theme(axis.text.y = element_text(size = 10, colour="black",
family="serif", angle=00)) +
theme(axis.title.x = element_text(size=10, colour="black",
family="serif", angle=00))+
theme(axis.title.y = element_text(size=10, colour="black",
family="serif", angle=90))
p
p1 <- p+theme(legend.title=element_blank())
p1
p2 <- p1+ theme(legend.key = element_blank())
p3 <- p2 + theme(legend.position="bottom")
p3 + scale_fill_discrete( #here is
the issue! It should be discrete and fill?!
breaks=c("Anab_ug_L", "Aph_ug_L", "Nod_ug_L"),
labels=c("Anabaena spp.", "Aphanizomenon sp.",
"N. spumigena"))
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@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]]
Hi Anna, You haven't mapped fill to anything. Scales only control how a mapping is translated into a visual property, you have to set the actual mapping as well. What do you want the fill color to indicate? Best, Ista On Wed, Jan 15, 2014 at 3:42 AM, Anna Zakrisson Braeunlich <anna.zakrisson at su.se> wrote:> Dear all, > > I have problems adding new names to the legend (not the legend title). > I have tried all sorts of versions involving scale_fill_discrete and themes. Weirdly, my method works fine when changing legend when making a barplot instead. > I have searched and tried and am soon throwing the computer out of the window. Please, help. I have read the help pages, but I don't understand why this won't work. > The plot produced below is extremely ugly. Ignore this. I just wanted to add the full script for my own data. With my own data the plot looks fine. > > # 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 > year = factor(rep(c("2007", "2008", "2009"), each = 45)), #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))) > > library(ggplot2) > p <- ggplot(mydata,aes(week,var1))+ > facet_grid(year~station, scales = "free")+ > scale_shape_manual(values = 1:3) + > geom_point(mapping=aes(shape=organism))+ > theme_bw() + > theme(strip.background = element_blank()) + > xlab("Week")+ > ylab(expression(paste("Biomass (", mu, "g ", L^-1, ")")))+ > geom_line(aes(group=organism, linetype=organism))+ > theme(strip.text.x = element_text(size = 10, colour="black", family="serif", angle=00)) + > theme(strip.text.y = element_text(size = 10, colour="black", family="serif", angle=90)) + > theme(axis.text.x = element_text(size = 10, colour="black", family="serif", angle=00)) + > theme(axis.text.y = element_text(size = 10, colour="black", family="serif", angle=00)) + > theme(axis.title.x = element_text(size=10, colour="black", family="serif", angle=00))+ > theme(axis.title.y = element_text(size=10, colour="black", family="serif", angle=90)) > p > > p1 <- p+theme(legend.title=element_blank()) > p1 > p2 <- p1+ theme(legend.key = element_blank()) > p3 <- p2 + theme(legend.position="bottom") > p3 + scale_fill_discrete( #here is the issue! It should be discrete and fill?! > breaks=c("Anab_ug_L", "Aph_ug_L", "Nod_ug_L"), > labels=c("Anabaena spp.", "Aphanizomenon sp.", > "N. spumigena")) > > 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. >