Given a dataframe df
and code to plot its subset sub1 <- df[, c('date', 'price', 'type')]
as follows:
df <- structure(list(date = c("2021-10-1", "2021-10-2", "2021-10-3",
"2021-10-4", "2021-10-5", "2021-10-6", "2021-10-7", "2021-10-8",
"2021-10-9", "2021-10-10", "2021-10-11", "2021-10-12", "2021-10-13",
"2021-10-14"), value = c(190.3, 174.9, 163.2, 168.4, 168.6,
168.2, 163.5, 161.6, 172.9, 166.5, 175.2, 197.7, 212.1, 177.9
), price = c(191.3, 175.9, 164.2, 169.4, 169.6, 169.2, 164.5,
162.6, 173.9, 167.5, 176.2, 198.7, 213.1, 178.9), quantity = c(192.3,
176.9, 165.2, 170.4, 170.6, 170.2, 165.5, 163.6, 174.9, 168.5,
177.2, 199.7, 214.1, 179.9), type = c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L)), class = "data.frame", row.names = c(NA,
-14L))
Code to plot:
df$date <- as.Date(df$date)
df$type <- as.factor(df$type)
df %>%
select(date, price, type) %>%
mutate(date = as.Date(date)) %>%
rename(value = price) %>%
ggplot(aes(x=date, y=value, group=type, color = type, fill = type))
geom_area(alpha=0.4, position = "identity")
theme(
text = element_text(size=20),
plot.margin=unit(c(1, 1, 1.5, 1.2), 'cm')
)
scale_y_continuous(breaks = range(df$value),
expand = expansion(mult = c(0, 0.1), add = c(1, 0)))
scale_x_date(breaks = range(df$date))
ggsave(filename = './{value}.png', device = 'png')
Out:
Now, I hope to iteratively generate multiple sub-dataframes, such as (please note date
and type
are two common columns):
sub1 <- df[, c('date', 'value', 'type')]
sub2 <- df[, c('date', 'price', 'type')]
sub3 <- df[, c('date', 'quantity', 'type')]
...
and loop all dataframes to plotting code, finally save them one by one using the name of value.png
, price.png
,...
How could I do that in R? Thanks a lot.
EDIT1:
lapply(c("value", "price", "quantity"), function(variable) {
png(paste0(variable, ".png"))
ggplot(aes(x=date, y=value, group=type, color = type, fill = type))
geom_area(alpha=0.4, position = "identity")
theme(
text = element_text(size=20),
plot.margin=unit(c(1, 1, 1.5, 1.2), 'cm')
)
scale_y_continuous(breaks = range(df$value),
expand = expansion(mult = c(0, 0.1), add = c(1, 0)))
scale_x_date(breaks = range(df$date))
dev.off()})
CodePudding user response:
I use for loop, it works, welcome to share other solutions:
df$date <- as.Date(df$date)
df$type <- as.factor(df$type)
cols <- c('value', 'price', 'quantity')
for (col in cols){
p <- df %>%
select(date, col, type) %>%
mutate(date = as.Date(date)) %>%
rename(value = col) %>%
ggplot(aes(x=date, y=value, group=type, color = type, fill = type))
geom_area(alpha=0.4, position = "identity")
theme(
text = element_text(size=20),
plot.margin=unit(c(1, 1, 1.5, 1.2), 'cm')
)
scale_y_continuous(breaks = range(df$value),
expand = expansion(mult = c(0, 0.1), add = c(1, 0)))
scale_x_date(breaks = range(df$date))
print(p)
}
Out: