Search code examples
rggplot2histogram

Plotting a histogram with ggplot2 when the magnitude of data is large


I have this following data frame, df for which I desire to plot a histogram.

     x
1   -28313937
2   -218616099
3   -18406124
4   20307666
5   31985283
6   41429217
7   46488567
8   47690792
9   51127321
10  53168291
11  55247883
12  -49200409
13  33398814
14  36198419
15  42765257
16  45857195
17  43870899
18  50557988
19  49574516
20  52317786
21  50769743

I use the following piece of code for plotting the histogram,

R_hist <- ggplot(df, aes(x=x)) + 
geom_histogram(binwidth=.5, colour="black", fill="white") + 
geom_vline(aes(xintercept=mean(x, na.rm=T)), color="violet", linetype="dashed", size=1)

When I tried to call the object R_hist, I get an Error : cannot allocate vector of size 4.1 Gb In addition: Warning messages: 1: In seq.default(round_any(range[1], size, floor), round_any(range[2], : Reached total allocation of 4021Mb: see help(memory.size)

Could someone please let me know why the histogram is not being plotted as it should here

Thanks.


Solution

  • as indicated in the comments, you're trying to plot a histogram with a bar from the min to max value in df$x.

    Instead, use geom_bar and stat='identity':

    # grab the data provied
    df <- read.table('clipboard')
    
    # switch the names cause it'll bug me
    df$y <- df$x
    df$x <- row.names(df)
    
    # plot using some identifier (row.names in this case)
    ggplot(df, aes(x=x, y=y)) + geom_bar(stat='