首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >循环遍历多个ggplot图

循环遍历多个ggplot图
EN

Stack Overflow用户
提问于 2018-03-06 22:20:16
回答 3查看 72关注 0票数 2

我有以下数据,我试图构建一个跨越3个股票符号的for loop

代码语言:javascript
复制
symbols <- c("HOG", "GE", "GOOG")

我有下面的ggplot。我想做两件事。

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

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

下面是一个使用GOOG的示例。

代码语言:javascript
复制
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

代码语言:javascript
复制
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")
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-06 22:29:34

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

这就是我的意思:

代码语言:javascript
复制
#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,后者是它循环遍历的对象。在title参数中,我刚刚使用了一个paste0操作来连接您想要的其他文本的符号名。

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

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

例如:

代码语言:javascript
复制
for(i in 1:3){

    print(plotList[[i]])

}

如果您一开始只想立即绘制图,就可以放弃plotList[[symbol]] <-位,只需将所有ggplot指令包装在print命令中,这将完成相同的任务。

祝好运!

票数 2
EN

Stack Overflow用户

发布于 2018-03-06 22:23:33

只需要做几处改动:

代码语言:javascript
复制
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")))
票数 1
EN

Stack Overflow用户

发布于 2018-03-06 22:40:11

我从写一个函数开始。我还会将数据转换为宽长格式,这与ggplot一起工作得更好:

代码语言:javascript
复制
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()
}

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

代码语言:javascript
复制
plotSymbol(NFO_WCAnalysis, "GOOG")

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

代码语言:javascript
复制
plots <- lapply(symbols, function(x) plotSymbol(NFO_WCAnalysis, x))

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49140946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档