簡體   English   中英

將直方圖從R導出到Excel

[英]Exporting Histogram from R to Excel

我在R中有一些要表示為直方圖的數據(實際上,我將有6個直方圖),然后將這些圖導出到excel文件中。 我剛剛使用了hist()函數,但是我也在嘗試ggplot2函數。 每個直方圖都有10,000個數據,因此我不能只導出原始數據並在excel中創建直方圖(我假設這樣做會導致生成一個大小可笑的excel文件,我不希望這樣)。

有什么方法可以導出圖形?

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")

另一種解決方案是將合並的數據存儲到數據幀中,然后通過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 = ",")

請注意,您也可以根據需要導出密度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM