【问题标题】:Plotting a histogram in ggplot2 results in memory error (only 418 data points)在 ggplot2 中绘制直方图会导致内存错误(仅 418 个数据点)
【发布时间】:2012-06-21 11:14:22
【问题描述】:

我正在尝试使用 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 的范围,这是可以理解的......

【问题讨论】:

    标签: r memory ggplot2 histogram


    【解决方案1】:

    您的代码应如下所示:

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

    原因是aes() 中的参数是在您的数据环境中评估的。这意味着您的映射应该指向数据中的列名,即x=Sepal.Length)。

    当您以您的方式编写 aes() 调用时,您试图告诉 ggplot 将 150 个不同的变量映射到 x,并且类似地将 150 个不同的变量映射到 fill - 这显然不是不是你想的那样。

    【讨论】:

    • 您好,感谢您改进我的代码的建议。事实证明,即使引用列名,我的数据仍然不会绘制。我认为这是由于数据的范围。对数据进行对数转换后,将绘制直方图。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多