I have list of indices
l <- list(a = sample(1:nrow(mpg), 10), b = sample(1:nrow(mpg), 15), c = sample(1:nrow(mpg), 10),
d = sample(1:nrow(mpg), 12), e = sample(1:nrow(mpg), 17))
I try to plot lines selected by indices stored in l on the same graph.
Is it possible to compactify next expression with purrr?
library(tidyverse)
ggplot() +
geom_line(data = mpg[l$a, ], aes(x = displ, y = cty), color = RColorBrewer::brewer.pal(5, "Set2")[1]) +
geom_line(data = mpg[l$b, ], aes(x = displ, y = cty), color = RColorBrewer::brewer.pal(5, "Set2")[2]) +
geom_line(data = mpg[l$c, ], aes(x = displ, y = cty), color = RColorBrewer::brewer.pal(5, "Set2")[3]) +
geom_line(data = mpg[l$d, ], aes(x = displ, y = cty), color = RColorBrewer::brewer.pal(5, "Set2")[4]) +
geom_line(data = mpg[l$e, ], aes(x = displ, y = cty), color = RColorBrewer::brewer.pal(5, "Set2")[5])

