7

I am plotting data from multiple dataframes in ggplot2 as follows:

# subset of iris data
vdf = iris[which(iris$Species == "virginica"),]
# plot from iris and from vdf
ggplot(iris) + 
   geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) + 
   geom_line(aes(x=Sepal.Width, y=Sepal.Length), colour="gray", size=2,
             data=vdf)

the legend for colour includes only entries from iris, and not from vdf. how can I make ggplot2 agg a legend from data=vdf, which in this case would be a gray line below the legend for iris? thanks.

agstudy
  • 116,828
  • 17
  • 186
  • 250
  • Now you can use `ggnewscale` this will let you add a new scale slot for colour/fill. Here for more information – RobertoSupe Aug 09 '20 at 02:24
  • @RobertoSupe Welcome to SO and thanks for your contribution. This is absolutely correct and why not make an answer out of this? That would be more helpful for most users as it makes this better visible. As a side, although this may not be important to you, you'll also gain a little bit of reputation:) – tjebo Aug 09 '20 at 10:58

2 Answers2

7

You should set the color as an aes to show it in the legend.

# subset of iris data
vdf = iris[which(iris$Species == "virginica"),]
# plot from iris and from vdf
library(ggplot2)
ggplot(iris) + geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
  geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour="gray"), 
            size=2, data=vdf)

enter image description here

EDIT I don't think you can't have a multiple legends for the same aes. here aworkaround :

library(ggplot2)
ggplot(iris) + 
  geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
  geom_line(aes(x=Sepal.Width, y=Sepal.Length,size=2), colour="gray",  data=vdf) +
   guides(size = guide_legend(title='vdf color'))

enter image description here

tjebo
  • 17,289
  • 5
  • 42
  • 72
agstudy
  • 116,828
  • 17
  • 186
  • 250
  • @agstudy: but I want the line to be gray and also for the legend to be separate. Note that when you do the above, it changes all the legends for the other species to be bold lines, even though the other species are not plotted bold so it looks confusing. Is there a way to get two legends? –  Jul 14 '13 at 18:52
  • 3
    @user248237dfsf agstudy is correct. **ggplot2** by design will not allow multiple legends for the same aesthetic (i.e. 2 colour legends, 2 fill legends, etc.). – joran Jul 14 '13 at 20:17
0

There are now plenty of threads about this topic, and I still hope that they are at some point all linked to one super answer.

This is now possible with ggnewscale.

library(ggplot2)
library(ggnewscale)

vdf = iris[which(iris$Species == "virginica"),]

ggplot(iris) + 
  geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) + 
  ggnewscale::new_scale_color()+
  ## then bring color into aesthetic, all else as per usual
  geom_line(aes(x=Sepal.Width, y=Sepal.Length, colour="gray"), size=2,
            data=vdf) +
  scale_color_manual(values = "grey")

Created on 2022-02-01 by the reprex package (v2.0.1)

tjebo
  • 17,289
  • 5
  • 42
  • 72