Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:12:53+00:00 2026-06-15T07:12:53+00:00

I have ~ 5 very large vectors (~ 108 MM entries) so any plot/stuff

  • 0

I have ~ 5 very large vectors (~ 108 MM entries) so any plot/stuff I do with them in R takes quite long time.

I am trying to visualize their distribution (histogram), and was wondering what would be the best way to superimpose their histogram distributions in R without taking too long. I am thinking to first fit a distribution to the histogram, and then plot all the distribution line fits together in one plot.

Do you have some suggestions on how to do that?

Let us say my vectors are:

x1, x2, x3, x4, x5.

I am trying to use this code: Overlaying histograms with ggplot2 in R

Example of the code I am using for 3 vectors (R fails to do the plot):

n = length(x1)
dat <- data.frame(xx = c(x1, x2, x3),yy = rep(letters[1:3],each = n))
ggplot(dat,aes(x=xx)) + 
    geom_histogram(data=subset(dat,yy == 'a'),fill = "red", alpha = 0.2) +
    geom_histogram(data=subset(dat,yy == 'b'),fill = "blue", alpha = 0.2) +
    geom_histogram(data=subset(dat,yy == 'c'),fill = "green", alpha = 0.2)

but it takes forever to produce the plot, and eventually it kicks me out of R. Any ideas on how to use ggplot2 efficiently for large vectors? Seems to me that I had to create a dataframe, of 5*108MM entries and then plot, highly inefficient in my case.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:12:56+00:00Added an answer on June 15, 2026 at 7:12 am

    Here’s a little snippet of Rcpp that bins data very efficiently – on my computer it takes about a second to bin 100,000,000 observations:

    library(Rcpp)
    cppFunction('
      std::vector<int> bin3(NumericVector x, double width, double origin = 0) {
        int bin, nmissing = 0;
        std::vector<int> out;
    
        NumericVector::iterator x_it = x.begin(), x_end;
        for(; x_it != x.end(); ++x_it) {
          double val = *x_it;
          if (ISNAN(val)) {
            ++nmissing;
          } else {
            bin = (val - origin) / width;
            if (bin < 0) continue;
    
            // Make sure there\'s enough space
            if (bin >= out.size()) {
              out.resize(bin + 1);
            }
            ++out[bin];
          }
        }
    
        // Put missing values in the last position
        out.push_back(nmissing);
        return out;
      }
    ')
    
    x8 <- runif(1e8)
    system.time(bin3(x8, 1/100))
    #   user  system elapsed 
    #  1.373   0.000   1.373 
    

    That said, hist is pretty fast here too:

    system.time(hist(x8, breaks = 100, plot = F))
    #   user  system elapsed 
    #  7.281   1.362   8.669 
    

    It’s straightforward to use bin3 to make a histogram or frequency polygon:

    # First we create some sample data, and bin each column
    
    library(reshape2)
    library(ggplot2)
    
    df <- as.data.frame(replicate(5, runif(1e6)))
    bins <- vapply(df, bin3, 1/100, FUN.VALUE = integer(100 + 1))
    
    # Next we match up the bins with the breaks
    binsdf <- data.frame(
      breaks = c(seq(0, 1, length = 100), NA),
      bins)
    
    # Then melt and plot
    binsm <- subset(melt(binsdf, id = "breaks"), !is.na(breaks))
    qplot(breaks, value, data = binsm, geom = "line", colour = variable)
    

    FYI, the reason I had bin3 on hand is that I’m working on how to make this speed the default in ggplot2 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large possible data set that I am trying to visualize
I have some large vectors, the data of them are coming from some calculations
I have a very large multidimensional vector that changes in size all the time.
I have a very large std::vector of std::vectors containing a fixed number of unsigned
I have a very large Excel sheet converted from a 6000 page PDF file,
I have a very large UIView approx 3000x3000 in size. In this large view
I have a very large table of over 9 million rows and in my
I have a very large ASP.NET application in C#. The issue is simple yet
I have a very large data file with around 60000 rows. I need to
I have a very large XML file which has like 40000 data, and when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.