Building a histogram in ggplot2 leads to a memory error (a total of 418 data points)

I am trying to build a histogram with two datasets using ggplot2. My dataset has 418 values ​​to plot, with two groups of data (so there will be two sets of color bars on my histogram). Annoyingly, I cannot reproduce the iris dataset issue:

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

This creates a subtle histogram. When I try this according to my data, I get:

 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" 

I have 16 GB of memory, so creating a patch with 418 data points should not be a problem.

Any help is greatly appreciated.


It turns out that my data will still not be displayed, even if it refers to column names. I think this is due to the data range. After the log converts the data, graph the histogram. It seems that ggplot2 or R as a whole does not like the range 1-165476109, which is understandable ...

+4
source share
1 answer

Your code should look like this:

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

enter image description here

The reason is that the arguments inside aes() are evaluated in your data environment. This means that your mapping should point to the column names in your data, i.e. x=Sepal.Length ).

When you write the aes() call the way you did, you try to tell ggplot match 150 different variables with x and likewise match 150 different variables with fill - this is obviously not what you meant.

+5
source

All Articles