在 R 中使用循环动态创建 ggplot 多个图表

问题描述 投票:0回答:2

我正在尝试使用 ggplot 批量构建多个图表。 这些图表都来自同一个数据帧,我唯一需要更改的是我观察到的指标(y 轴)。

我现在所做的事情远没有效率,因为我正在复制粘贴每个图表的 ggplot 设置,并且只是更改需要修改的指标。

我知道有一种方法,可以通过列出所有需要动态更改的指标来动态创建这些图表,这样我只需要在循环中使用 ggplot 一次。

我想过去我设法使用

eval()
get()
甚至
assign()
来做到这一点,但我不记得我是如何做到的。

理想情况下,最终我会根据指标创建不同的图表,每个图表都有一个唯一的名称:

Forecast_Monthly_Visits_FR_Graph
Forecast_Monthly_Sales_FR_Graph
等。 下面是一个可重现的示例。 非常感谢。

library(ggplot2)

Date <- as.Date(c('2022-01-01','2022-01-02','2022-01-03','2022-01-01','2022-01-02','2022-01-03'))
Type <- c("Actual", "Actual", "Actual", "Forecast", "Forecast", "Forecast")
Visits <- c(67398,63398,61398,53422,72726,92822)
Sales <- c(17398,23398,41398,12422,33726,53822)
Actual_Forecast_Monthly_France <- data.frame(Date , Type , Visits, Sales)

Forecast_Monthly_Visits_FR_Graph <-
  ggplot(data=Actual_Forecast_Monthly_France, aes(x=Date, y=Visits, group=Type,  linetype = factor(Type) , show.legend = FALSE)) +
  geom_line(aes(color=Type)) +
  geom_point(size = 0.5) +
  geom_text(aes(label=round(Visits)), size = 3) +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title = "Visits") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_color_manual(values= c("#03a623", "#030063", "#ffaad7", "#b6b6b6"))

Forecast_Monthly_Visits_FR_Graph

 Forecast_Monthly_Sales_FR_Graph <-
  ggplot(data=Actual_Forecast_Monthly_France, aes(x=Date, y=Sales, group=Type,  linetype = factor(Type) , show.legend = FALSE)) +
  geom_line(aes(color=Type)) +
  geom_point(size = 0.5) +
  geom_text(aes(label=round(Sales)), size = 3) +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title = "Sales") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_color_manual(values= c("#03a623", "#030063", "#ffaad7", "#b6b6b6"))

Forecast_Monthly_Sales_FR_Graph 
r dataframe loops ggplot2 eval
2个回答
5
投票

与@Peter的方法类似,但我将列名称作为字符串传递,恕我直言,这使得使用例如循环遍历列变得更容易

lapply
。请注意,当我将列名称作为字符串传递时,我使用
.data
代词:

library(ggplot2)

plot_fun <- function(x) {
  ggplot(data = Actual_Forecast_Monthly_France, aes(x = Date, y = .data[[x]], group = Type, linetype = factor(Type), show.legend = FALSE)) +
    geom_line(aes(color = Type)) +
    geom_point(size = 0.5) +
    geom_text(aes(label = round(Visits)), size = 3) +
    theme(axis.text.x = element_text(angle = 90)) +
    labs(title = x) +
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_color_manual(values = c("#03a623", "#030063", "#ffaad7", "#b6b6b6"))
}

cols <- names(Actual_Forecast_Monthly_France)[!names(Actual_Forecast_Monthly_France) %in% c("Date", "Type")]
names(cols) <- cols

lapply(cols, plot_fun)
#> $Visits

#> 
#> $Sales


2
投票

您可以创建一个包含要绘制的 y 变量的函数。然后,创建一个 for 循环来循环多个 y 变量选项就变得相对简单,或者您可以使用

purrr
中的函数。现在包含了循环,我必须沿着@stefan 解释的引用变量名称的路线走下去。

library(ggplot2)
library(purrr)

gg_fun <- function(y_var){
  Forecast_Monthly_Visits_FR_Graph <-
    ggplot(data=Actual_Forecast_Monthly_France, aes(x=Date, y=!!sym(y_var), group=Type,  linetype = factor(Type) , show.legend = FALSE)) +
    geom_line(aes(color=Type)) +
    geom_point(size = 0.5) +
    geom_text(aes(label=round(Visits)), size = 3) +
    theme(axis.text.x = element_text(angle = 90)) +
    labs(title = y_var) +
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_color_manual(values= c("#03a623", "#030063", "#ffaad7", "#b6b6b6"))
  
  return(Forecast_Monthly_Visits_FR_Graph)
  
}

gg <- map(c("Visits", "Sales"), gg_fun)


gg[[1]]

gg[[2]]

创建于 2022-09-13,使用 reprex v2.0.2

数据

Date <- as.Date(c('2022-01-01','2022-01-02','2022-01-03','2022-01-01','2022-01-02','2022-01-03'))
Type <- c("Actual", "Actual", "Actual", "Forecast", "Forecast", "Forecast")
Visits <- c(67398,63398,61398,53422,72726,92822)
Sales <- c(17398,23398,41398,12422,33726,53822)
Actual_Forecast_Monthly_France <- data.frame(Date , Type , Visits, Sales)

创建于 2022-09-13,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.