代码之家  ›  专栏  ›  技术社区  ›  Nico

如何绘制列表列表

  •  1
  • Nico  · 技术社区  · 7 年前

    我有一张单子。这是列表外观的示例。

        [[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
    

    问题是如何在ggplot中为列表中的每个数据帧绘制Prtf\u返回?。以下是我目前得到的:

    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)))
    }
    

    This is how the plot looks

    到目前为止,我知道的是图例可能与行本身不匹配,但我真的不知道如何更改代码以使其正常工作。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Rui Barradas    7 年前

    诀窍是 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]]
    

    enter image description here


    这个列表列表重复了问题中的两个数据帧。

    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))
    
        2
  •  0
  •   Nico    7 年前

    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)
    

    之后,该图返回每个分位数的一个很好的累积和
    CumsumPlot

    推荐文章