editing legend items

115 views
Skip to first unread message

Falko

unread,
Mar 29, 2012, 6:37:04 AM3/29/12
to ggp...@googlegroups.com
Hi there ggplot-Folks!

I'm quite new to "R" and "ggplot" and I have an issue regarding the legend editing. Here is my code. I know, it might not be that elegant it could be, but it works fine for me (but if you have any further suggestions - sharing them would be highly appreciated)

First of all I load my raw-data, which I gained from an RTCA-experiment:

library(gplots)

Now that I have my data, I've got to rename the columns:

colnames(mcpd2)<-c("Time",
                   "Hours",
                   "2M1000_A",
                   "2M1000_B",
                   "2M1000_C",
                   "2M0500_A",
                   "2M0500_B",
                   "2M0500_C",
                   "2M0250_A",
                   "2M0250_B",
                   "2M0250_C",
                   "2M0100_A",
                   "2M0100_B",
                   "2M0100_C",
                   "2M0050_A",
                   "2M0050_B",
                   "2M0050_C",
                   "2M0025_A",
                   "2M0025_B",
                   "2M0025_C",
                   "2M0010_A",
                   "2M0010_B",
                   "2M0010_C",
                   "2M0001_A",
                   "2M0001_B",
                   "2M0001_C"
                   )

Now, I have to normalize to a certain timepoint by typing:

mcpd2.s<-
as.data.frame(
sweep(
data.matrix(mcpd2),
2,
data.matrix(mcpd2)[93,]
)
)

That done, I pick the concentrations I'd like to plot:

pick<-c("Time","01")
picked<-unique(
grep(
paste(
pick,
collapse="|"
),
colnames(mcpd2.s),
value=TRUE
)
)
toPlot<-mcpd2.s[picked]

After that I melt the data obtained and make a colsplit with the reshape2-package:

melt.M2.a<-melt(toPlot,id.var="Time")
melt.M2<-
cbind(
melt.M2.a,
reshape2::colsplit(melt.M2.a$variable,"_",c("Treat","Replicate"))
)

Now the plot can be created:

library(ggplot)
plot.M2<-ggplot(data=melt.M2,aes(x=Time,y=value,group=Treat,color=Treat,fill=Treat))
plot.M2+
  stat_summary(fun.data="mean_cl_normal",geom="smooth",size=1,alpha=0.1)+
  scale_x_continuous(limits=c(0,48))

And here is where I got stuck. How can I edit the legend, the legend title itself and also what is written within the legend. Is there a way to change "2M0001" into "1µM". By adding the code line:

+guides(fill=guide_legend(title="title"))

it just gives me a second legend with the desired title but doesn't replace the actual one. Also, the colors don't match with the graph and the legend items are not renamed. Am I missing something?

Any hint is highly appreciated.

Regards,
Falko

Dennis Murphy

unread,
Mar 29, 2012, 11:09:07 AM3/29/12
to Falko, ggp...@googlegroups.com
Hi:

First of all, thank you for the reproducible example.

Scales are one of the more difficult features of ggplot2 to master,
and a few things are going on here that you need to learn about:

(i) You can define labels in a separate vector or as labels in a
factor() statement; since you want expressions, the former is
advisable in this case.
(ii) When you map multiple aesthetics to the same variable and want
to use a single legend, you need to modify all legends with a suitable
scale_*() function, one for each aesthetic defined.
(iii) You can modify a title either by picking a suitable variable
name for the factor of interest (which will be the default legend
title) or you can change it manually inside a scale_*() function.

Here's a way to get what I think you want with the legend:

# Create a factor variable for treatment rather than a character
variable so that you can control the ordering of the levels:
melt.M2$treat <- factor(melt.M2$Treat, levels = c('2M0001', '2M0010', '2M0100'))

# Create a list of expressions that can be passed into the scale
functions as the labels = argument:
labls <- list(expression(paste('1', mu, 'M', sep = "")),
expression(paste('10', mu, 'M', sep = "")),
expression(paste('100', mu, 'M', sep = "")))

ggplot(data = melt.M2, aes(x = Time, y = value, color = treat, fill = treat)) +
stat_summary(fun.data="mean_cl_normal", geom="smooth", size=1, alpha=0.1) +
scale_x_continuous(limits = c(0, 48)) +
scale_color_manual('My title', breaks = levels(melt.M2$treat),
values = c('blue', 'green', 'orange'),
labels = labls) +
scale_fill_manual('My title', breaks = levels(melt.M2$treat),
values = c('blue', 'green', 'orange'),
labels = labls)

Notice that both scale_color_manual() and scale_fill_manual() needed
to be used in order for the two legends to merge. More specifically,
the two legends need the same title, the same breaks, the same values
and the same labels for the merger to take place.

You don't need guide_legend() here unless you want to change aspects
of the legend that are under its control, which are described on its
help page.

HTH,
Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Reply all
Reply to author
Forward
0 new messages