2

I have a data frame that explicitly contains aesthetic information explicitly:

df <- structure(list(
  tpr = c(0.57, 0.1, 0.02, 0, 0, 1, 0.11, 0.03, 0.01, 0, 1, 0.1, 0.04, 0.01, 0, 1, 0.12, 0.03, 0, 0),
  ppv = c(0.07, 0.3, 0.54, 0.38, NaN, 0.03, 0.24, 0.28, 0.5, NaN, 0.03, 0.14, 0.28, 0.25, NaN, 0.03, 0.22, 0.36, 0.3, NaN),
  model = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("model1", "model2", "model3", "model4"), class = "factor"),
  alpha = c(1, 1, 1, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1),
  col = c("steelblue", "steelblue", "steelblue", "steelblue", "steelblue", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red", "red"),
  size = c(1, 1, 1, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1)),
  .Names = c("tpr", "ppv", "model", "alpha", "col", "size"),
  class = c("data.frame"), row.names = c(NA, -20L))

I'm plotting my data using

ggplot(
    ddf,
    aes(x= tpr, y = ppv,  group = model, col = col, alpha = alpha, size = size)
    ) +
    geom_line()+
    scale_x_continuous(breaks = seq(0,1,by=.1), minor_breaks = seq(0,1, by = .05))+
    scale_y_continuous(breaks = seq(0,1, by = .1), limits = c(0,1))+
    scale_color_identity()+
    scale_alpha_identity()+
    scale_size_identity()+
    theme_minimal()

plot1

However, I can't figure out how to add a single legend that shows all the aesthetics with the corresponding group label. I'm expecting something like

enter image description here but with the correct aesthetics per group (i.e. color, size, alpha in this case).

I'm aware of this answer, but I would like to stay away from specifying manual scales and instead use the scales provided within the data itself.

Is this possible in ggplot2? How?

Community
  • 1
  • 1
hdkrgr
  • 1,666
  • 1
  • 12
  • 22
  • 1
    If you use `scale_colour_identity(guide="legend")` you will get a legend, but the levels will be labeled "red" and "steelblue". So then, is there some way to designate labels of "model1", "model2" etc.? I don't know, but if it's possible it will certainly be just as complicated as using `scale_*_manual` with named vectors of values. By the way, the `guide="legend"` thing triggers an error if used with alpha and size scales, not clear why that should be a problem. – bdemarest Apr 26 '17 at 03:45
  • Have you tried using base plotting functions? It's easy to construct a legend manually with base, but really hard (or not possible) with ggplot2. – bdemarest Apr 26 '17 at 03:48
  • @bdemarest Regarding "_triggers an error_", I posted [an issue](https://github.com/tidyverse/ggplot2/issues/2112). – Henrik Apr 26 '17 at 10:08
  • Thanks for the input. I had also played around with the `guide` and `label` things (and encountered the same error :) ) but couldn't get it to work as I wanted either. I used the `scale_*_manual` way for now (including making sure stuff appears in the right order etc). I guess explicitly supplying the aesthetics in the data is simply not a very GoG-way of doing things, nevertheless I think creating legends for (multiple) `scale_*_identity`s should be possible. @Henrik, thanks for opening the issue. – hdkrgr Apr 26 '17 at 10:46

1 Answers1

-1

Something like this, maybe.

ggplot(df, aes(x = tpr, y = ppv, colour = model, linetype = model))+
geom_line()
Rafael Cunha
  • 239
  • 2
  • 6