基于字符串向量在ggplot中绘制不同的指标

plot different metrics in ggplot based on a string vector

这会生成一个图:

diamonds %>% 
  ggplot(aes(x = cut, y = x, color = factor(color))) +
  geom_line() +
  facet_wrap(clarity ~ .)

我想对钻石 x、y、z 的每个特征执行相同的操作。而不是复制粘贴此函数 3 次并在 aes() 内更改 y = 我正在尝试使用 map:

编写循环
metrics <- diamonds %>% select(x:z) %>% names

# for each metric name string in metrics, want the above plot but witht he y = to be the corresponding metric:
## tried
metrics %>% map(ggplot(diamonds, aes(x = cut, y = .x, color = factor(color))) + geom_line() + facet_wrap(clarity ~ .))

给出:

Error: Can't convert a gg/ggplot object to function

如何遍历指标中的每个项目并为每个项目生成上面的图?

可能这会有所帮助,您缺少波浪符号,另外,由于 .x 是字符串,我们也需要对其进行评估,我已经使用了 get inside,让我知道这是否不能解决您的问题,谢谢:

metrics %>% 
  map(~ggplot(diamonds, aes(x = cut, y = get(.x), color = factor(color))) + geom_line() + facet_wrap(clarity ~ .))