Дан кадр данных df следующим образом:

structure(list(date = c("2018-1-31", "2018-2-28", "2018-3-31", 
"2018-4-30", "2018-5-31", "2018-6-30", "2018-7-31", "2018-8-31", 
"2018-9-30", "2018-10-31", "2018-11-30", "2018-12-31", "2019-1-31", 
"2019-2-28", "2019-3-31", "2019-4-30", "2019-5-31", "2019-6-30", 
"2019-7-31", "2019-8-31", "2019-9-30", "2019-10-31", "2019-11-30", 
"2019-12-31", "2020-1-31", "2020-2-29", "2020-3-31", "2020-4-30", 
"2020-5-31", "2020-6-30", "2020-7-31", "2020-8-31", "2020-9-30", 
"2020-10-31", "2020-11-30", "2020-12-31"), v1 = c(8.6, 8.8, 8.2, 
8.3, 8.3, 8, 8.5, 8.2, 8.3, 8, 8, 8.1, 8.4, 8, 8.6, 8.5, 8.5, 
8.5, 8.1, 8.2, 8.4, 8.4, 8.2, 8.7, 8.4, 8.8, 10.1, 11.1, 11.1, 
11.1, 10.7, 10.4, 10.9, 10.5, 10.7, 10.1), v2 = c(13.4336, 13.3883, 
12.6903, 12.653, 12.1991, 11.7613, 11.4851, 11.5161, 11.2084, 
10.79, 10.3072, 10.2597, 10.8518, 10.6457, 11.1621, 10.8055, 
11.025, 11.1934, 10.8487, 10.6762, 10.6664, 10.5819, 10.6692, 
10.6928, 10.7, 10.7, 11.5, 12, 12.5, 12.8, 12.9, 13.3, 13.5, 
13.7, 13.6, 13.3)), class = "data.frame", row.names = c(NA, -36L
))

Я могу построить и сохранить годовые цифры для v1 с помощью кода ниже:

library(ggrepel)
library(ggplot2)

df$date <- as.Date(df$date, format="%Y-%m-%d")
df$year <- format(df$date, "%Y")
df$month <- format(df$date, "%m")

df$month <- as.numeric(as.character(df$month))

ggsave <- function(...) {
  ggplot2::ggsave(...)
  invisible()
}

ggplot(aes(x = month, y = v1, color = year, linetype = year, shape = year), data = df) + 
  geom_point() +
  geom_line() +
  ylab('v1') +
  xlab('') +
  scale_x_continuous(breaks = df$month, labels = df$month) +
  scale_y_continuous(labels = function(x) paste0(x, "%")) +
  geom_text_repel(aes(label= scales::percent(v1/100, accuracy = 0.1)), size=4, show_guide = F) +
  ggsave('./v1.png', width = 10, height = 6)

Вне:

enter image description here

Теперь я надеюсь использовать столбцы цикла v1, v2 и т. Д. Для построения и сохранения цифр для каждой переменной:

colNames <- names(df)
for (colName in colNames[2:3]){
  ggplot(aes(x = month, y = colName, color = year, linetype = year, shape = year), data = df) + 
  geom_point() +
  geom_line() +
  ylab(colName) +
  xlab('') +
  scale_x_continuous(breaks = df$month, labels = df$month) +
  scale_y_continuous(labels = function(x) paste0(x, "%")) +
  geom_text_repel(aes(label= scales::percent(colName/100, accuracy = 0.1)), size=4, show_guide = F) +
  ggsave(paste('./', colName, '.png', sep = ''), width = 10, height = 6)
}

Но возникает ошибка: Error in colName/100 : non-numeric argument to binary operator.

Как я мог это сделать правильно? Спасибо.

0
ah bon 9 Окт 2021 в 08:49

2 ответа

Лучший ответ

Вы можете использовать местоимение .data -

colNames <- names(df)

for (colName in colNames[2:3]){
    ggplot(aes(x = month, y = .data[[colName]], color = year, 
               linetype = year, shape = year), data = df) + 
      geom_point() +
      geom_line() +
      ylab(colName) +
      xlab('') +
      scale_x_continuous(breaks = df$month, labels = df$month) +
      scale_y_continuous(labels = function(x) paste0(x, "%")) +
      geom_text_repel(aes(label= scales::percent(.data[[colName]]/100, 
                      accuracy = 0.1)), size=4, show_guide = F) +
      ggsave(paste('./', colName, '.png', sep = ''), width = 10, height = 6)
  }
1
Ronak Shah 9 Окт 2021 в 06:58

Мы можем преобразовать в sym bol и оценить с помощью !!

colNames <- names(df)
for (colName in colNames[2:3]){
    ggplot(aes(x = month, y = !! sym(colName), color = year, 
               linetype = year, shape = year), data = df) + 
      geom_point() +
      geom_line() +
      ylab(colName) +
      xlab('') +
      scale_x_continuous(breaks = df$month, labels = df$month) +
      scale_y_continuous(labels = function(x) paste0(x, "%")) +
      geom_text_repel(aes(label= scales::percent(!! sym(colName))/100, 
                      accuracy = 0.1)), size=4, show_guide = F) +
      ggsave(paste('./', colName, '.png', sep = ''), width = 10, height = 6)
  }
0
akrun 9 Окт 2021 в 17:11