简体   繁体   English

在 R 中绘制图形并在循环中将其导出到 Excel

[英]Plotting a graph in R and exporting it to Excel in a loop

In my data I have multiple countries and each country has 5 products.在我的数据中,我有多个国家,每个国家有 5 个产品。 I performed a regression for each combination and now I am trying to plot the predicted values vs. the actual values on the same graph, for each combination.我对每个组合进行了回归,现在我试图在同一图表上绘制每个组合的预测值与实际值。

I have 10 countries in total and each country has its own tab in an Excel file.我总共有 10 个国家/地区,每个国家/地区在 Excel 文件中都有自己的选项卡。 Since there are 5 products, this is a total of 50 graphs.由于有 5 个产品,因此总共有 50 个图表。 I want to plot the graphs in R and export them to Excel in a loop.我想在 R 中绘制图形并在循环中将它们导出到 Excel。 I am using excel.link package ggplot and the issue that I'm having is that the graphs appear as blank space in Excel or if there was some graph in R - that graph appears instead of the required graph.我正在使用excel.linkggplot ,我遇到的问题是图表在 Excel 中显示为空白,或者如果 R 中有一些图表 - 该图表出现而不是所需的图表。

**Note: I used to get an error with the first graph that said "rversion not found" but now I updated my RStudio and I no longer get this error. **注意:我曾经在第一个图表中遇到错误,显示“未找到 rversion”,但现在我更新了我的 RStudio 并且不再出现此错误。 The graph still gets exported blank or the previous graph appears instead图形仍然导出为空白或出现上一个图形

Here is a simplified loop, similar to the one that is in my code.这是一个简化的循环,类似于我的代码中的循环。 If I run the loop manually, by changing i each time, everything gets exported OK.如果我手动运行循环,通过每次更改i ,一切都可以导出。 If I run the for loop , the issues happen that I described above:如果我运行for loop ,就会发生我上面描述的问题:

require(excel.link)

set.seed(124)

for(i in 1:5){

# i <- 2
myseq <- seq(1,100, by=1)
norm <- rnorm(100)

mydata <- as.data.frame(cbind(myseq, norm))
colnames(mydata)
ggplot(data = mydata, aes(x=myseq, y=norm, group=1)) + geom_line(size=1, col="blue") +
  ggtitle(paste("My Plot ", i))

y.plot=current.graphics()
xl[a1] = list(y.plot)

}

First of all you should print explicitly ggplot2 graphics in the loop.首先,您应该在循环中明确打印 ggplot2 图形。 Second, you put plots in excel in the same sheet in the same position.其次,您将 Excel 中的绘图放在同一工作表的同一位置。 So your graphs are placed one on each other and you will see only last plot in Excel.因此,您的图表彼此重叠,您将在 Excel 中只看到最后一个图。 Code for displaying plots on separate sheets:在单独的工作表上显示图的代码:

library(excel.link)
library(ggplot2)

set.seed(124)
xl.workbook.add() # open new workbook
for(i in 1:5){

    myseq <- seq(1,100, by=1)
    norm <- rnorm(100)

    mydata <- as.data.frame(cbind(myseq, norm))
    colnames(mydata)
    p = ggplot(data = mydata, aes(x=myseq, y=norm, group=1)) +
        geom_line(size=1, col="blue") +
        ggtitle(paste("My Plot ", i))

    print(p) ## display ggplot graphics
    y.plot=current.graphics()
    xl.sheet.add() ## add new sheet in excel workbook
    xl[a1] = y.plot

}

Code for displaying plots on the single sheet from top to down:在单张纸上从上到下显示绘图的代码:

library(excel.link)
library(ggplot2)

set.seed(124)
xl.workbook.add() # open new workbook
y.plot = lapply(1:5, function(i) {

    myseq <- seq(1,100, by=1)
    norm <- rnorm(100)

    mydata <- as.data.frame(cbind(myseq, norm))
    colnames(mydata)
    p = ggplot(data = mydata, aes(x=myseq, y=norm, group=1)) + 
        geom_line(size=1, col="blue") +
        ggtitle(paste("My Plot ", i))

    print(p) ## display ggplot graphics
    current.graphics()

})

xl[a1] = y.plot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM