【问题标题】:plotting and loop in RR中的绘图和循环
【发布时间】:2019-03-26 03:53:21
【问题描述】:

您将如何编写一个循环在单独的图中为每个模型绘制 mpg vs cyl 和 mpg vs vs?谢谢。 PS:这只是一个示例数据集,我有 100 多个模型,所以肯定需要一个循环。

enter image description here

【问题讨论】:

标签: r loops plot


【解决方案1】:

不确定这正是您想要实现的目标。是不是这样的:

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"]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 2017-02-09
    相关资源
    最近更新 更多