诀窍是
rbind
Quantile
子列表中的数据帧。然后调用
lapply
rotate_x_text
从程序包
ggpubr
已使用。
library(ggplot2)
library(ggpubr)
plotFun <- function(DF){
DF[["Quantile"]] <- factor(DF[["Quantile"]])
ggplot(data = DF, aes(x = Date, y = cumsum(Prtf_Return),
group = Quantile, colour = Quantile)) +
geom_line() +
rotate_x_text(angle = 45) +
scale_color_discrete(name = "Quantile",
labels = as.character(1:10))
}
first_year_rbind <- lapply(first_year, function(L) do.call(rbind, L))
p_list <- lapply(first_year_rbind, plotFun)
p_list[[1]]
这个列表列表重复了问题中的两个数据帧。
df1 <-
structure(list(Prtf_Return = c(-0.08161658, 0.05648458, 0.24751328,
0.26955881, 0.08099889, 0.14633871, -0.02790529, -0.17913224,
0.33681922, 0.23411797, 0.10671685, 0.19845169, 0.11399025, 0.10308543,
0.01388617, 0.03685517, 0.0966141), Quantile = c(8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L),
Date = structure(1:17, .Label = c("2002-07-31",
"2003-07-31", "2004-07-30", "2005-07-29", "2006-07-31", "2007-07-31",
"2008-07-31", "2009-07-31", "2010-07-30", "2011-07-29", "2012-07-31",
"2013-07-31", "2014-07-31", "2015-07-31", "2016-07-29", "2017-07-31",
"2018-07-31"), class = "factor")), class = "data.frame",
row.names = c("2002-07-31",
"2003-07-31", "2004-07-30", "2005-07-29", "2006-07-31", "2007-07-31",
"2008-07-31", "2009-07-31", "2010-07-30", "2011-07-29", "2012-07-31",
"2013-07-31", "2014-07-31", "2015-07-31", "2016-07-29", "2017-07-31",
"2018-07-31"))
df2 <-
structure(list(Prtf_Return = c(-0.02322572, 0.25252339, 0.26290503,
0.26407951, 0.03501359, 0.13907544, -0.02064978, -0.27060352,
0.33156794, 0.22488091, 0.03268992, 0.29199203, 0.10818639, 0.19940041,
0.04085818, 0.04345668, 0.11842907), Quantile = c(9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L),
Date = structure(1:17, .Label = c("2002-07-31",
"2003-07-31", "2004-07-30", "2005-07-29", "2006-07-31", "2007-07-31",
"2008-07-31", "2009-07-31", "2010-07-30", "2011-07-29", "2012-07-31",
"2013-07-31", "2014-07-31", "2015-07-31", "2016-07-29", "2017-07-31",
"2018-07-31"), class = "factor")), class = "data.frame",
row.names = c("2002-07-31",
"2003-07-31", "2004-07-30", "2005-07-29", "2006-07-31", "2007-07-31",
"2008-07-31", "2009-07-31", "2010-07-30", "2011-07-29", "2012-07-31",
"2013-07-31", "2014-07-31", "2015-07-31", "2016-07-29", "2017-07-31",
"2018-07-31"))
first_year <- list(list(df1, df2), list(df1, df2))