【发布时间】:2021-11-28 21:35:03
【问题描述】:
给定一个数据框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)
输出:
现在我希望使用for循环列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。
我怎样才能正确地做到这一点?谢谢。
【问题讨论】: