如何使用 ggplot 和 purrr 在同一图表上绘制由数据框中的索引选择的线?
How to plot lines selected by indices from dataframe on the same graph with ggplot and purrr?
我有索引列表
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))
我尝试在同一张图上绘制存储在 l
中的索引选择的线。
是否可以使用 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])
您可以构建一个新的数据集来绘制数据:
df = numeric() #Empty starting point
for(i in 1:length(l)){
df = rbind(df, cbind(mpg[l[[i]],], name=names(l)[i]))}
#Rbind the last iteration with the mpg on the indexes + a column with the name of the index
ggplot(df, aes(x=displ, y=cty, color=name)) +
geom_line()
您可以使用 scale_color_brewer()
更改调色板。
输出:
调出数据框的相关行,并提供相应名称的列:
library(tidyverse)
data(mpg)
mpg[unlist(l),] %>%
mutate(data = rep(names(l),lengths(l))) %>%
ggplot() +
geom_line(aes(x = displ, y = cty,color = data)) +
scale_color_manual(values=RColorBrewer::brewer.pal(5, "Set2"))
library(tidyverse)
l <- list(a = sample(1:nrow(mpg), 10), b = sample(1:nrow(mpg), 10), c = sample(1:nrow(mpg), 10),
d = sample(1:nrow(mpg), 10), e = sample(1:nrow(mpg), 10))
# Generate a list of ggplot layers to add to the final plot
layer_list <- map2(.x = l,
.y = seq_along(l),
.f = ~ geom_line(data = mpg[.x, ], aes(x = displ, y = cty),
color = RColorBrewer::brewer.pal(5, "Set2")[.y]))
# Build the final plot
ggplot(data = mpg) +
layer_list
我有索引列表
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))
我尝试在同一张图上绘制存储在 l
中的索引选择的线。
是否可以使用 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])
您可以构建一个新的数据集来绘制数据:
df = numeric() #Empty starting point
for(i in 1:length(l)){
df = rbind(df, cbind(mpg[l[[i]],], name=names(l)[i]))}
#Rbind the last iteration with the mpg on the indexes + a column with the name of the index
ggplot(df, aes(x=displ, y=cty, color=name)) +
geom_line()
您可以使用 scale_color_brewer()
更改调色板。
输出:
调出数据框的相关行,并提供相应名称的列:
library(tidyverse)
data(mpg)
mpg[unlist(l),] %>%
mutate(data = rep(names(l),lengths(l))) %>%
ggplot() +
geom_line(aes(x = displ, y = cty,color = data)) +
scale_color_manual(values=RColorBrewer::brewer.pal(5, "Set2"))
library(tidyverse)
l <- list(a = sample(1:nrow(mpg), 10), b = sample(1:nrow(mpg), 10), c = sample(1:nrow(mpg), 10),
d = sample(1:nrow(mpg), 10), e = sample(1:nrow(mpg), 10))
# Generate a list of ggplot layers to add to the final plot
layer_list <- map2(.x = l,
.y = seq_along(l),
.f = ~ geom_line(data = mpg[.x, ], aes(x = displ, y = cty),
color = RColorBrewer::brewer.pal(5, "Set2")[.y]))
# Build the final plot
ggplot(data = mpg) +
layer_list