【问题标题】:For loop across multiple ggplot graphsFor循环跨越多个ggplot图
【发布时间】:2018-03-06 22:20:16
【问题描述】:

我有以下数据,我正在尝试构建一个跨越 3 个股票代码的 for loop

symbols <- c("HOG", "GE", "GOOG")

我有以下ggplot。我正在尝试做两件事。

1) 对symbols 中的所有三个符号运行ggplot

2) 更改每个 ggplot 的标题以包含正确的符号名称

下面是使用GOOG 的示例。

library(ggplot2)
ggplot(subset(NFO_WCAnalysis, Ticker %in% c("GOOG"))) +
  geom_line(aes(Date, NFO, group = Ticker, colour = "Blue")) +
  geom_line(aes(Date, WC, group = Ticker, colour = "Red")) +
  labs(title="NFO and WC plot GOOG", x="Date", y="NFO / WC") +
  scale_color_manual(labels = c("NFO", "WC"), values = c("Blue", "Red")) +
  theme_bw() +
  guides(color=guide_legend("Legend"))

数据。 (如有必要,我可以在for loop 发布我的尝试)我也愿意只使用基本绘图功能而不是ggplot

structure(list(Ticker = c("GOOG", "GOOG", "GOOG", "GOOG", "GE", 
"GE", "GE", "GE", "HOG", "HOG", "HOG", "HOG"), Date = c(2017, 
2016, 2015, 2014, 2017, 2016, 2015, 2014, 2017, 2016, 2015, 2014
), REC = c(18705, 14232, 13909, 10849, 24438, 24076, 27022, 23237, 
2435.65, 2361.37, 2300.99, 2164.26), INV = c(749, 268, 0, 0, 
21923, 22354, 22515, 17689, 538.2, 499.92, 585.91, 448.87), OtherCurrentAssetsNotCash = c(80, 
596, 628, 852, 0, 0, 0, 0, 223.37, 227.06, 323.59, 370.96), Payables = c(3137, 
2041, 1931, 1715, 15153, 14435, 13680, 12067, 227.6, 235.32, 
235.61, 196.87), SpontaneousFunsIncDeftaxes = c(21476, 14941, 
14343, 13813, 19514, 18867, 17943, 14854, 529.82, 486.65, 471.97, 
449.32), NFO = c(-5079, -1886, -1737, -3827, 11694, 13128, 17914, 
14005, 2439.8, 2366.38, 2502.91, 2337.9), LTD = c(3969, 3935, 
1995, 3228, 110555, 105497, 147742, 190999, 4587.26, 4666.98, 
4832.47, 3761.53), EQ = c(152502, 139036, 120331, 103860, 64264, 
75827, 98273, 128158, 1844.28, 1920.16, 1839.65, 2909.29), OtherLongTermLiabilities = c(16211, 
7544, 5636, 4562, 144423, 119843, 165573, 238451, 382.97, 440.55, 
553.55, 468), FA = c(72987, 62089, 57347, 50531, 377945, 365183, 
493071, 654954, 6087.93, 6036.39, 5995.1, 5580.01), WC = c(99695, 
88426, 70615, 61119, -58703, -64016, -81483, -97346, 726.58, 
991.3, 1230.57, 1558.81), CreditPlusCashMinus = c(-104774, -90312, 
-72352, -64946, 70397, 77144, 99397, 111351, 1713.22, 1375.08, 
1272.34, 779.090000000001)), .Names = c("Ticker", "Date", "REC", 
"INV", "OtherCurrentAssetsNotCash", "Payables", "SpontaneousFunsIncDeftaxes", 
"NFO", "LTD", "EQ", "OtherLongTermLiabilities", "FA", "WC", "CreditPlusCashMinus"
), row.names = c(NA, -12L), class = "data.frame")

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我认为这里最好的方法是创建一个 ggplot 对象列表,然后将文本替换为您当前拥有的文本。

    这就是我的意思:

    #This will store all our ggplot objects, not necessary, but this gives us some extra flexibility
    plotList <- list()
    
    #This will loop through each of the symbol names
    for(symbol in symbols){
    
        #What you had before, but with minor changes
        plotList[[symbol]] <- ggplot(subset(NFO_WCAnalysis, Ticker %in% symbol)) +
          geom_line(aes(Date, NFO, group = Ticker, colour = "Blue")) +
          geom_line(aes(Date, WC, group = Ticker, colour = "Red")) +
          labs(title=paste0("NFO and WC plot ", symbol), x="Date", y="NFO / WC") +
          scale_color_manual(labels = c("NFO", "WC"), values = c("Blue", "Red")) +
          theme_bw() +
          guides(color=guide_legend("Legend"))
    
    }
    

    请注意,绘图命令的所有不同之处在于“GOOG”已被替换为symbol,这是它循环通过的对象。在标题参数中,我刚刚使用了paste0 操作将符号名称与您想要的其他文本连接起来。

    使用存储在plotList[[symbol]] 中的对象,您现在可以使用plotList[["GOOG"]] 或任何您想要的符号名称来调用它们:)

    因为它是一个列表,所以你也可以按顺序使用它,例如plotList[[1]]。这意味着如果您想稍后再打印一些东西,也可以循环打印,或者只是抓取您想要的东西。

    例如

    for(i in 1:3){
    
        print(plotList[[i]])
    
    }
    

    如果您只想一开始就立即绘制,您可以放弃 plotList[[symbol]] &lt;- 位并将所有 ggplot 指令包装在 print 命令中,这样就可以完成同样的工作。

    祝你好运!

    【讨论】:

    • 这应该会得到更多的认可,非常有帮助!
    • 嘿!感谢您的反馈。超级高兴看到这仍然在帮助人们。祝你的项目好运:)
    【解决方案2】:

    只需要几处更改:

    for(symbol in symbols)
      print(ggplot(subset(NFO_WCAnalysis, Ticker %in% symbol)) +
              geom_line(aes(Date, NFO, group = Ticker, colour = "Blue")) +
              geom_line(aes(Date, WC, group = Ticker, colour = "Red")) +
              labs(title= paste("NFO and WC plot", symbol), x = "Date", y = "NFO / WC") +
              scale_color_manual(labels = c("NFO", "WC"), values = c("Blue", "Red")) +
              theme_bw() +
              guides(color = guide_legend("Legend")))
    

    【讨论】:

    • 谢谢!当然在这里学到了一些新东西!我总是用for(i in 1:length(symbols)){.... 开始循环并遇到很多问题!再次感谢!
    • 显示ggplot也可以有多动态,在labs(title...中简单添加symbol没想到会这么干净!
    • @user113156,这也是一种有效的方法,在这种情况下,您可以在我有symbol 的地方写symbols[i]。但是for(symbol in symbols) 确实看起来更好一些。
    【解决方案3】:

    我先写一个函数。我还会将数据从宽长格式转换,这与 ggplot 配合得更好:

    library(tidyverse)
    
    plotSymbol <- function(data, symbol) {
      data %>%
        filter(Ticker == symbol) %>%
        select(Date, NFO, WC) %>%
        gather(variable,value, -Date) %>%
        ggplot(aes(Date, value)) + 
          geom_line(aes(color = variable, group = variable)) +
          labs(title = paste("NFO and WC plot", symbol),
               y = "NFO, WC") +
          scale_color_manual(values = c("blue", "red")) +
          theme_bw()
    }
    

    现在你可以运行了,例如:

    plotSymbol(NFO_WCAnalysis, "GOOG")
    

    您可以使用lapply 生成ggplot 对象列表,而不是循环,每个符号一个:

    plots <- lapply(symbols, function(x) plotSymbol(NFO_WCAnalysis, x))
    

    现在您可以使用列表plots 做任何您想做的事情。例如,通过对代码进行一些修改,您可以使用符号作为文件名的一部分写入 PNG 文件。

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      相关资源
      最近更新 更多