To get a legend, you need to map aes (such as shape, color, linetype). In general, we need a legend if we want to differentiate data by variable (such as gender, group, etc), for example, aes(x=x, y=y, shape=group), then a legend is automatically created (note: variable (for this example, “group”) should be in the dataset in ggplot() call)
However, if I read your question, you don’t need to differentiate by variable, but still want to show what point and line means. In that case, one way to work around would be:
#make fake data (factor level 1) for dist1 and dist2
data.df$dist11 <- as.factor(rep(1, dim(data.df)[1]))
data.df$dist22 <- data.df$dist11 #just copy from dist11
# Create plot
my.plot1 <- ggplot(data.df, aes(x=TAD, y=dist1)) + geom_point(aes(shape=dist11)) +
geom_line(aes(x=TAD, y=dist2, linetype=dist22)) +
facet_grid(XID ~ FID, labeller=label_both) +
labs(x = "TAD", y = "Response")
Is this what you expected….?
Let me know if you have more questions…
Kaori
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example:
https://github.com/hadley/devtools/wiki/Reproducibility
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Dear Maziar,
There should be a way to put 2 legends closer together, but I need to search how to do it (so I don’t have quick answer…).
However, to change the legend title and labels, try this:
my.plot1 + scale_shape_manual(name="", values=16, labels="text for dist1") +
scale_linetype_manual(name="", values=1, labels="text for dist2")
--
opts(legend.position = c(0.85, 0.5))
Dear Maziar,
For opts() , as the warning said, it is “deprecated” in the recent versions of ggplot2 (>0.9.2) . You can simply replace it with theme() in your code.
I think for your situation, “import it into PowerPoint and manually create my own legend” is probably the best since you don’t have variable to be differentiated by mapping (aesthetics).
In general, ggplot2 can do lot of things for you without much effort J
Kaori
From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com]
On Behalf Of maziar
Sent: Thursday, May 16, 2013 4:21 PM
To: ggp...@googlegroups.com
Subject: Re: ggplot legend problem with facet_grid
Dear Kaori,
--
Dear Maziar,
I am sorry I was not clear enough. Mapping aes means, you map some variable (which is available in the dataset (eg, column name “group” contains “A”, “B”)) to some specific aes, for example, color (such as “red”, ”blue”, for “A”,”B”). We can’t map “red” color for group “A” , circle shape for group “B” if you don’t map aes, ggplot doesn’t create a legend.
See the difference with the simple example below.
Data <- data.frame(x1 = 1:10,
y1 = 0.5*1:10 + rnorm(10),
y2 = 0.5*1:10 + rnorm(10),
group=rep(factor(c("A","B")),
each=5))
#this is you are currently doing (no legend)
ggplot(Data, aes(x=x1, y=y1)) + geom_point() +
geom_line(aes(y=y2))
#map aes (color)----different colors are used for group
ggplot(Data, aes(x=x1, y=y1, color=group)) + geom_point() +
geom_line(aes(y=y2))
#map shape for point---different shape is used
ggplot(Data, aes(x=x1, y=y1, color=group)) + geom_point(aes(shape=group), size=3) +
geom_line(aes(y=y2))
I hope this explains a bit more what I meant in the previous reply….
Kaori
From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com]
On Behalf Of maziar
Sent: Thursday, May 16, 2013 4:55 PM
To: ggp...@googlegroups.com
Subject: Re: ggplot legend problem with facet_grid
Kaori,
--