簡體   English   中英

在ggplot2中繪制直方圖會導致內存錯誤(僅418個數據點)

[英]Plotting a histogram in ggplot2 results in memory error (only 418 data points)

我正在嘗試使用ggplot2繪制具有2組數據的直方圖。 我的數據集有418個繪圖值,有2組數據(因此我的直方圖上會有2組彩色條)。 令人討厭的是我無法使用iris數據集重現問題:

library(ggplot2)
ggplot(iris, aes(x=iris[,1], fill=iris[,5])) + 
geom_histogram(binwidth=.5,alpha=.5)

這樣可以創建直方圖。 當我在我的數據上嘗試時,我得到:

Error : cannot allocate vector of size 4.0 Gb
In addition: Warning messages:
1: In anyDuplicated.default(breaks) :
  Reached total allocation of 16366Mb: see help(memory.size)
2: In anyDuplicated.default(breaks) :
  Reached total allocation of 16366Mb: see help(memory.size)
3: In anyDuplicated.default(breaks) :
  Reached total allocation of 16366Mb: see help(memory.size)
4: In anyDuplicated.default(breaks) :
  Reached total allocation of 16366Mb: see help(memory.size)
Error in UseMethod("scale_dimension") : 
  no applicable method for 'scale_dimension' applied to an object of class "NULL"

我有16GB的內存,所以產生418個數據點的情節應該不是問題。

任何幫助非常感謝。


事實證明,即使引用列名,我的數據仍然不會繪制。 我認為這是由於數據的范圍。 在對數據進行對數轉換之后,直方圖繪制。 似乎ggplot2或R作為一個整體不喜歡1-165476109的范圍,這是可以理解的......

您的代碼應如下所示:

ggplot(iris, aes(x=Sepal.Length, fill=Species)) + 
  geom_histogram(binwidth=.5,alpha=.5)

在此輸入圖像描述

原因是aes()中的參數在數據環境中進行評估。 這意味着您的映射應指向數據中的列名,即x=Sepal.Length )。

當你以你的方式編寫aes()調用時,你試圖告訴ggplot將150個不同的變量映射到x ,同樣地映射150個不同的變量來fill - 這顯然不是你想到的。

暫無
暫無

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

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