rggplot2out-of-memorygeom-raster

R ggplot - Can't allocate big vector


I'm trying to plot a relatively small data set, and I can't get it to show me the plot. It keeps giving the error Error: cannot allocate vector of size 9.7 Gb. This doesn't make much sense to me as the data set is rather small.

> nrow(locs)
[1] 130
> head(locs)
       STATION AVGTRANGE     LAT     LONG
1: USC00286979  22.13333 40.6971 -75.2042
2: USC00360022  21.33333 40.5361 -79.8152
3: USC00360132  24.37037 40.5227 -78.3694
4: USC00360140  19.80000 40.4949 -78.4667
5: USC00360147  22.36667 41.3585 -77.9262
6: USC00360457  20.68000 40.8209 -76.4983

How I'm plotting it.

gg <- ggplot(data = locs, aes(x = LONG, y = LAT)) +
  geom_raster(aes(fill=AVGTRANGE), interpolate=TRUE)
gg # can't allocate here

Here is the dput my data.

> dput(locs)
structure(list(STATION = structure(1:130, .Label = c("USC00286979", 
"USC00360022", "USC00360132", "USC00360140", "USC00360147", "USC00360457", 
"USC00360560", "USC00360656", "USC00360754", "USC00360785", "USC00360861", 
"USC00360868", "USC00361139", "USC00361212", "USC00361301", "USC00361350", 
"USC00361354", "USC00361362", "USC00361377", "USC00361480", "USC00361485", 
"USC00361705", "USC00361726", "USC00361751", "USC00361802", "USC00361810", 
"USC00361838", "USC00361920", "USC00362071", "USC00362183", "USC00362323", 
"USC00362470", "USC00362574", "USC00362721", "USC00362942", "USC00363018", 
"USC00363028", "USC00363226", "USC00363311", "USC00363321", "USC00363343", 
"USC00363417", "USC00363437", "USC00363451", "USC00363632", "USC00363665", 
"USC00363698", "USC00364214", "USC00364325", "USC00364432", "USC00364763", 
"USC00364778", "USC00364815", "USC00364839", "USC00364896", "USC00364934", 
"USC00364976", "USC00364992", "USC00365050", "USC00365109", "USC00365344", 
"USC00365573", "USC00365686", "USC00365738", "USC00365902", "USC00365918", 
"USC00366111", "USC00366151", "USC00366194", "USC00366238", "USC00366508", 
"USC00366649", "USC00366886", "USC00366921", "USC00366927", "USC00367029", 
"USC00367073", "USC00367103", "USC00367167", "USC00367186", "USC00367229", 
"USC00367409", "USC00367477", "USC00367732", "USC00367782", "USC00367863", 
"USC00367931", "USC00367938", "USC00368073", "USC00368184", "USC00368308", 
"USC00368361", "USC00368400", "USC00368449", "USC00368469", "USC00368596", 
"USC00368668", "USC00368868", "USC00368873", "USC00368888", "USC00368905", 
"USC00369298", "USC00369367", "USC00369408", "USC00369823", "USR0000PALL", 
"USW00003761", "USW00004726", "USW00004751", "USW00004787", "USW00004843", 
"USW00013739", "USW00014711", "USW00014712", "USW00014736", "USW00014737", 
"USW00014751", "USW00014762", "USW00014770", "USW00014777", "USW00014778", 
"USW00014860", "USW00054737", "USW00054782", "USW00054786", "USW00054789", 
"USW00054792", "USW00093778", "USW00094732", "USW00094823"), class = "factor"), 
    AVGTRANGE = c(22.1333333333333, 21.3333333333333, 24.3703703703704, 
    19.8, 22.3666666666667, 20.68, 23.35, 21.4333333333333, 25.75, 
    23.4333333333333, 23.6428571428571, 26.4333333333333, 27.551724137931, 
    25.3448275862069, 25.0666666666667, 26.6842105263158, 23.4444444444444, 
    29.6, 23.3, 30.2631578947368, 27.0454545454545, 25.9333333333333, 
    24.2083333333333, 27.448275862069, 28.2333333333333, 21.4666666666667, 
    24.1111111111111, 25.7333333333333, 23.8571428571429, 21.6, 
    26.08, 26.2916666666667, 27.1034482758621, 28.3666666666667, 
    27.9259259259259, 23.6, 25.7, 26.3666666666667, 26.0344827586207, 
    20.2666666666667, 23.0909090909091, 27.2727272727273, 25.9666666666667, 
    24.8214285714286, 20.2413793103448, 24.0333333333333, 20.6333333333333, 
    26.0344827586207, 22.6, 29.0333333333333, NA, 25.625, 19.0333333333333, 
    18.7666666666667, 21.0689655172414, 22, 24.1333333333333, 
    25.0333333333333, 24.0666666666667, 24.3666666666667, 20.7333333333333, 
    32.5, 26.6666666666667, NA, 22.2666666666667, 25.1333333333333, 
    27.1481481481481, 22.7, 24.4827586206897, 21.6071428571429, 
    20.8461538461538, 29.9333333333333, 17.3928571428571, 26.2666666666667, 
    23.84, 23.1481481481481, 23.8275862068966, 26.9, 26.7931034482759, 
    25.3636363636364, NA, 23.5333333333333, 27.3571428571429, 
    17.2, 24.5, 22.0666666666667, NA, 23.8333333333333, 26.5172413793103, 
    27.6551724137931, 21.2307692307692, 26.5384615384615, 19.5, 
    20.8, 25.3, 18.6666666666667, 25.2758620689655, 23.8333333333333, 
    24.3461538461538, 27.6551724137931, 25.7666666666667, 24, 
    26.0344827586207, 24.6, 28.7333333333333, 27.7, 20.1034482758621, 
    18.6071428571429, 26.1785714285714, 22.5714285714286, 22.6071428571429, 
    17.1785714285714, 19.3571428571429, 21.6071428571429, 24.4285714285714, 
    23.6071428571429, 21.6785714285714, 19.9642857142857, 25.2142857142857, 
    22.7241379310345, 23.0357142857143, 17.8928571428571, 22.2962962962963, 
    21.2857142857143, 21.8571428571429, 21, 25.6428571428571, 
    25.6071428571429, 19.4444444444444, 22.6785714285714), LAT = c(40.6971, 
    40.5361, 40.5227, 40.4949, 41.3585, 40.8209, 40.8619, 39.9355, 
    41.0072, 40.3803, 40.3916, 41.8975, 40.8415, 41.6516, 41.5217, 
    39.848, 39.9353, 41.9301, 40.1468, 41.0489, 41.1922, 39.7994, 
    39.9969, 41.3575, 41.775, 41.7391, 41.9903, 40.2258, 40.46, 
    40.1275, 41.5216, 40.4681, 40.50194, 40.0136, 40.71306, 41.1184, 
    41.4004, 39.8815, 41.5631, 40.0962, 40.5513, 40.9666, 40.2305, 
    39.78333, 40.5511, 39.77056, 40.2817, 40.5972, 41.4992, 41.6767, 
    40.0499, 40.1167, 41.4234, 40.1692, 40.3333, 40.8223, 40.9474, 
    40.5864, 41.64583, 41.131, 40.8344, 40.3391, 39.7808, 41.6725, 
    40.6475, 40.5319, 40.412, 40.61417, 40.1482, 40.075, 39.8, 
    41.9245, 39.9587, 40.8729, 40.12, 41.7394, 39.7275, 41.8157, 
    40.6515, 41.589, 40.9248, 41.3299, 41.4196, 39.8958, 40.5101, 
    40.683, 40.7831, 40.335, 40.05889, 41.05583, 39.8582, 41.8162, 
    40.5711, 40.7933, 41.40389, 41.008, 40.8532, 41.8975, 41.4792, 
    41.63, 41.7511, 41.84667, 39.89861, 41.7004, 40.0417, 41.4864, 
    39.8593, 40.31611, 41.8, 41.17833, 41.62639, 39.87327, 40.1962, 
    40.36667, 40.29639, 40.64985, 40.21722, 40.35472, 40.82056, 
    41.3336, 41.2433, 42.0803, 40.12028, 40.23833, 40.33, 41.13889, 
    41.04667, 39.91806, 40.08194, 40.4846), LONG = c(-75.2042, 
    -79.8152, -78.3694, -78.4667, -77.9262, -76.4983, -75.6428, 
    -77.2577, -76.4482, -76.0274, -79.8594, -78.7144, -79.9163, 
    -76.8463, -77.4478, -79.5898, -77.6394, -79.297, -79.8986, 
    -77.9411, -79.4361, -79.3665, -79.5963, -79.2172, -78.0417, 
    -77.971, -77.1567, -77.1894, -76.8703, -79.4058, -76.4043, 
    -78.7289, -80.0833, -78.3653, -79.5144, -75.7277, -79.8305, 
    -77.3506, -78.6014, -75.7513, -80.2167, -78.5871, -75.4354, 
    -79.9166, -75.9913, -77.0325, -76.8703, -79.1186, -80.4681, 
    -78.8036, -76.2742, -76.4333, -76.4933, -79.1411, -76.4667, 
    -75.6962, -76.8786, -77.5692, -80.425, -77.4336, -76.1352, 
    -79.8604, -79.041, -75.0641, -80.3861, -80.2172, -79.7245, 
    -79.7191, -74.953, -76.0717, -76.05, -78.0072, -75.1728, 
    -78.2161, -75.5011, -75.4465, -79.913, -78.2873, -78.5551, 
    -75.3303, -79.2825, -77.7381, -78.7493, -76.3948, -79.5459, 
    -79.6684, -76.8617, -75.313, -77.5213, -80.06, -77.4774, 
    -80.4249, -75.2781, -77.8672, -78.0183, -75.1876, -76.7891, 
    -77.1419, -79.4432, -79.693, -76.443, -79.1494, -80.1655, 
    -77.3871, -78.5278, -79.1025, -75.7861, -78.8338, -78.6333, 
    -78.8988, -80.215, -75.2267, -76.7724, -75.9666, -78.3202, 
    -75.4477, -76.8513, -79.9216, -76.8641, -75.7269, -76.9217, 
    -80.1824, -76.2944, -75.5572, -75.1225, -75.3794, -78.4116, 
    -76.8741, -75.0111, -80.2144)), .Names = c("STATION", "AVGTRANGE", 
"LAT", "LONG"), class = c("data.table", "data.frame"), row.names = c(NA, 
-130L), .internal.selfref = <pointer: 0x2a40128>, sorted = "STATION")

Solution

  • I am not sure what your are trying to achieve with the geom_raster, as your data does not seem to fit the purpose.

    Consider the outputs of dot plot:

    gg <- ggplot(data = locs, aes(x = LONG, y = LAT, colour = AVGTRANGE)) +
      geom_point()
      #geom_raster(aes(fill=AVGTRANGE), interpolate=TRUE)
    gg 
    
    data(faithfuld)
    gg <- ggplot(faithfuld, aes(waiting, eruptions, colour = density)) +
      geom_point()
      #geom_raster(aes(fill = density), interpolate = TRUE)
    gg
    

    I have also tried geom_contour on your data and it does not work:

    gg <- ggplot(data = locs, aes(x = LONG, y = LAT, z = AVGTRANGE)) +
      geom_contour()
    gg 
    

    UPDATE

    I have checked the code of the geom_raster and the reason it tries to create the giant plot is that resolution of the plot is based on the minimal distance between points. As some of the points in your data are quite close to each other size of the matrix is so large.

    If you round LAT and LONG to 2 digits code works.

    locs$LAT <- round(locs$LAT, 0)
    locs$LONG <- round(locs$LONG, 0)
    

    enter image description here