【问题标题】:Exporting Histogram from R to Excel将直方图从 R 导出到 Excel
【发布时间】:2013-07-30 13:43:35
【问题描述】:

我在 R 中有一些数据,我想将其表示为直方图(实际上,我将有 6 个直方图),然后将这些图导出到 Excel 文件中。我一直在使用 hist() 函数,但我也在尝试使用 ggplot2 函数。 每个直方图都有 10,000 条数据,所以我不能只导出原始数据并在 excel 中创建直方图(我假设这会导致我不想要的大小可笑的 excel 文件)。

有什么方法可以导出我的图表吗?

【问题讨论】:

  • ?png?ggsave。如果您真的需要在 Excel 中使用它们(为什么要这样做?),请将 PNG(或您选择的任何格式)导入 Excel。
  • @Roland 把它写下来作为答案。我想我们中的许多人都会同意导出和导入是解决这类问题的方法。
  • 有没有办法让r代码将.png导入excel?

标签: r excel graph export histogram


【解决方案1】:

excel.link 包是 RDCOMClient 的包装器。

以下示例仅适用于普通 RGui(不是 RStudio)的 R 3.0.1 中的我。

# load package
require(excel.link)

# plot something
plot(cos)

# save graph to tmp file
cos.plot=current.graphics()

# add excel workbook
xl.workbook.add()

# add sheet to excel workbook
xl.sheet.add()

# put your graph starting at the top left in cell A1
xl[a1]=list("Cosine plotting",cos.plot,"End of cosine plotting")

【讨论】:

    【解决方案2】:

    另一种解决方案是将分箱数据存储到数据框中,并通过 un CSV 文件或任何其他格式将数据框导出到 excel。

    > x  <- rnorm(1000)
    > h  <- hist(x)
    > h
    $breaks
     [1] -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5  3.0  3.5  4.0
    
    $counts
     [1]   1   5  23  38 104 154 208 191 130  85  39  17   4   0   1
    
    $density
     [1] 0.002 0.010 0.046 0.076 0.208 0.308 0.416 0.382 0.260 0.170 0.078 0.034 0.008 0.000 0.002
    
    $mids
    [1] -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25  0.25  0.75  1.25  1.75  2.25  2.75  3.25  3.75
    
    $xname
    [1] "x"
    
    $equidist
    [1] TRUE
    
    attr(,"class")
    [1] "histogram"
    > out  <- data.frame(mid = h$mids, counts = h$counts)
    > write.table(out, file = "export.csv", row.names = FALSE, sep = ",")
    

    请注意,您也可以根据需要导出密度。

    【讨论】:

      猜你喜欢
      • 2022-06-22
      • 2013-10-25
      • 2015-07-02
      • 2016-10-18
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多