Rolf Turner
2019-Dec-01 00:04 UTC
[R] Adding a legend to a (multi-facet) plot produced by ggplot().
I have been struggling to add a legend as indicated in the subject line, with no success at all. I find the help to be completely bewildering. I have attached the code of what I have tried in the context of a simple reproducible example. I have also attached a pdf file of a plot produced with base graphics to illustrate roughly what I am after. I would be grateful if someone could point me in the right direction. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: reprex.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20191201/38741f81/attachment.txt> -------------- next part -------------- A non-text attachment was scrubbed... Name: whatIwant.pdf Type: application/pdf Size: 6661 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20191201/38741f81/attachment.pdf>
Rui Barradas
2019-Dec-01 14:03 UTC
[R] Adding a legend to a (multi-facet) plot produced by ggplot().
Hello,
See if this is it. The standard trick is to reshape the data from wide
to long, see the SO post [1]. Then add a scale_shape_* layer to the plot.
yyy <- cbind(xxx, y3 = y3)
long <- reshape2::melt(yyy, id.vars = c("x", "y1",
"grp"))
ggplot(long, aes(x, y = value, colour = variable, shape = variable)) +
geom_line(aes(y = y1)) +
geom_point() +
scale_colour_manual("Doesn't
work",values=c("blue","red"),
labels=c("clyde","irving")) +
scale_shape_manual("Doesn't work",values=c(16,3),
labels=c("clyde","irving")) +
facet_grid(cols=vars(grp))
[1]
https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format
Hope this helps,
Rui Barradas
?s 00:04 de 01/12/19, Rolf Turner escreveu:>
> I have been struggling to add a legend as indicated in the subject line,
> with no success at all.? I find the help to be completely bewildering.
>
> I have attached the code of what I have tried in the context of a simple
> reproducible example.
>
> I have also attached a pdf file of a plot produced with base graphics to
> illustrate roughly what I am after.
>
> I would be grateful if someone could point me in the right direction.
>
> cheers,
>
> Rolf Turner
>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
Rolf Turner
2019-Dec-02 01:07 UTC
[R] Adding a legend to a (multi-facet) plot produced by ggplot().
On 2/12/19 3:03 am, Rui Barradas wrote:> Hello, > > See if this is it. The standard trick is to reshape the data from wide > to long, see the SO post [1]. Then add a scale_shape_* layer to the plot. > > > yyy <- cbind(xxx, y3 = y3) > long <- reshape2::melt(yyy, id.vars = c("x", "y1", "grp")) > > ggplot(long, aes(x, y = value, colour = variable, shape = variable)) + > ? geom_line(aes(y = y1)) + > ? geom_point() + > ? scale_colour_manual("Doesn't work",values=c("blue","red"), > ????????????????????? labels=c("clyde","irving")) + > ? scale_shape_manual("Doesn't work",values=c(16,3), > ????????????????????? labels=c("clyde","irving")) + > > ? facet_grid(cols=vars(grp)) > > > [1] > https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format > > > Hope this helps,Almost there. However the colour of the line changes with "values" and lines show up in the legend. I want the lines to be black in all facets, and only points to show up in the legend. I fiddled about a bit trying to achieve this but only succeeded in messing things up completely. Can you guide me a bit further, please? cheers, Rolf -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276