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()
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
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?
