Given a dataframe df
as follows:
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
))
I'm able to plot and save yearly figures for v1
with code below:
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)
Out:
Now I hope to use for loop columns v1
, v2
, etc. to plot and save figures for each variables:
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)
}
But it raises an error: Error in colName/100 : non-numeric argument to binary operator
.
How could I do that correctly? Thanks.
CodePudding user response:
You can use the .data
pronoun -
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)
}