For跨多个ggplot图循环 [英] For loop across multiple ggplot graphs

查看:88
本文介绍了For跨多个ggplot图循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据,我试图在3个股票代码中构建一个for loop

I have the following data and I am trying to construct a for loop across 3 stock symbols;

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

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

I have the following ggplot. I am trying to do two things.

1)在symbols

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

2) Change the title of each ggplot to incorporate the correct symbols name

下面使用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.

The data. (I can post my attempts at the for loop if necessary) I am also open to just using the base plot functionality instead of 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")

推荐答案

我认为这里最好的方法是创建一个ggplot对象的列表,然后仅替换当前内容中的文本即可.

I think the best approach here is to create a list of ggplot objects, then just replace the text in what you currently have.

这是我的意思:

#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操作将符号名称与所需的其他文本连接在一起.

Notice all that is different in the plotting command is that the "GOOG" has been replaced with symbol, which is the object it loops through. In the title argument I've just used a paste0 operation to concatenate what symbol name with the other text you want.

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

With objects stored in plotList[[symbol]] you can now call them however you want by using plotList[["GOOG"]] or whatever the name of the symbol you want is :)

由于它是一个列表,因此您也可以按顺序使用它,例如plotList[[1]].这意味着,如果您以后想要打印,也可以循环打印,也可以只打印想要的东西.

Because it's a list, you can also use it sequentially, e.g. plotList[[1]]. This means you can loop through to print things too if you want to later, or just grab the ones you want.

例如

for(i in 1:3){

    print(plotList[[i]])

}

如果您只是想立即绘制图形,则可以放弃plotList[[symbol]] <-位,只需将所有ggplot指令包装在print命令中,即可完成相同的工作.

If you just want to plot things immediately in the first place, you can ditch the plotList[[symbol]] <- bit and just wrap all the ggplot instructions within a print command, and that will do the same job.

祝你好运!

这篇关于For跨多个ggplot图循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆