【发布时间】:2019-03-26 03:53:21
【问题描述】:
您将如何编写一个循环在单独的图中为每个模型绘制 mpg vs cyl 和 mpg vs vs?谢谢。 PS:这只是一个示例数据集,我有 100 多个模型,所以肯定需要一个循环。
【问题讨论】:
-
你试过 ggplot 和 facet_wrap 吗?希望能给你带来好的结果。
-
这可能对stackoverflow.com/a/52045613有帮助
您将如何编写一个循环在单独的图中为每个模型绘制 mpg vs cyl 和 mpg vs vs?谢谢。 PS:这只是一个示例数据集,我有 100 多个模型,所以肯定需要一个循环。
【问题讨论】:
不确定这正是您想要实现的目标。是不是这样的:
data("mtcars")
library(tidyverse)
plots <- mtcars %>%
rownames_to_column("model") %>%
mutate(model = str_extract(model, "^[A-Za-z]+")) %>%
gather(key = "feature", value = "value", wt, vs) %>%
group_by(model) %>%
do(
plots = ggplot(., aes(x = mpg, y = value)) +
geom_point() +
facet_wrap(~feature, scales = "free_y") +
ggthemes::theme_few() +
ggtitle(sprintf("Model: %s", .$model))
) %>%
as.list()
plots <- set_names(plots[["plots"]], plots[["model"]])
plots[["Merc"]]
【讨论】: