我有一个清单清单。这是一个列表外观的示例。
[[7]][[8]]
Prtf_Return Quantile Date
2002-07-31 -0.08161658 8 2002-07-31
2003-07-31 0.05648458 8 2003-07-31
2004-07-30 0.24751328 8 2004-07-30
2005-07-29 0.26955881 8 2005-07-29
2006-07-31 0.08099889 8 2006-07-31
2007-07-31 0.14633871 8 2007-07-31
2008-07-31 -0.02790529 8 2008-07-31
2009-07-31 -0.17913224 8 2009-07-31
2010-07-30 0.33681922 8 2010-07-30
2011-07-29 0.23411797 8 2011-07-29
2012-07-31 0.10671685 8 2012-07-31
2013-07-31 0.19845169 8 2013-07-31
2014-07-31 0.11399025 8 2014-07-31
2015-07-31 0.10308543 8 2015-07-31
2016-07-29 0.01388617 8 2016-07-29
2017-07-31 0.03685517 8 2017-07-31
2018-07-31 0.09661410 8 2018-07-31
[[7]][[9]]
Prtf_Return Quantile Date
2002-07-31 -0.02322572 9 2002-07-31
2003-07-31 0.25252339 9 2003-07-31
2004-07-30 0.26290503 9 2004-07-30
2005-07-29 0.26407951 9 2005-07-29
2006-07-31 0.03501359 9 2006-07-31
2007-07-31 0.13907544 9 2007-07-31
2008-07-31 -0.02064978 9 2008-07-31
2009-07-31 -0.27060352 9 2009-07-31
2010-07-30 0.33156794 9 2010-07-30
2011-07-29 0.22488091 9 2011-07-29
2012-07-31 0.03268992 9 2012-07-31
2013-07-31 0.29199203 9 2013-07-31
2014-07-31 0.10818639 9 2014-07-31
2015-07-31 0.19940041 9 2015-07-31
2016-07-29 0.04085818 9 2016-07-29
2017-07-31 0.04345668 9 2017-07-31
2018-07-31 0.11842907 9 2018-07-31
列表的第一部分表示开始月份,第二部分表示分位数([[“” Start_month“]] [[” Quantile“]])。
问题是如何为列表中的每个数据帧在ggplot中绘制Prtf_Returns?这是到目前为止我得到的:
library(ggplot2)
p <- ggplot()
for(i in 1:10){
p <- p + geom_line(data = first_year[[1]][[i]], aes(x = Date, y = cumsum(Prtf_Return), group = Quantile, colour = Quantile)) +
scale_color_discrete(name = "Quantile", labels = rep(as.character(1:10)))
}
到目前为止,我所知道的是图例可能与行本身不匹配,但我真的不知道如何更改代码以使其正常工作。
预先感谢
答案 0 :(得分:0)
诀窍是在子列表中rbind
个数据帧中的每个Quantile
。然后在lapply
循环中调用绘图函数。 plot函数可以是匿名函数,但是为了清楚起见,我将其单独编写。
我还旋转x轴标签。为此,使用软件包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))
答案 1 :(得分:0)
我知道了。感谢每个人的帮助。 诀窍是(就像Rui提到的那样)用积木创建一个新列。
first_year_cumsum <- lapply(first_year, function(a) lapply(a, function(b) ddply(b, .(Quantile), transform, cumsum = cumsum(Prtf_Return))))
那样,Rui的功能如下:
plotFun <- function(DF){
DF[["Quantile"]] <- factor(DF[["Quantile"]])
ggplot(data = DF, aes(x = Date, y = cumsum,
group = Quantile, colour = Quantile)) +
geom_line() +
theme_bw() +
rotate_x_text(angle = 45) +
scale_color_discrete(name = "Quantile",
labels = as.character(1:10))
}
first_year_rbind <- lapply(first_year_cumsum, function(L) do.call(rbind, L))
p_list <- lapply(first_year_rbind, plotFun)
再次感谢