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 1017257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:40:32+00:00 2026-05-16T10:40:32+00:00

I am using R and I have two data frames: carrots and cucumbers. Each

  • 0

I am using R and I have two data frames: carrots and cucumbers. Each data frame has a single numeric column that lists the length of all measured carrots (total: 100k carrots) and cucumbers (total: 50k cucumbers).

I wish to plot two histograms—carrot length and cucumbers lengths—on the same plot. They overlap, so I guess I also need some transparency. I also need to use relative frequencies not absolute numbers since the number of instances in each group is different.

Something like this would be nice, but how can I create it from my two tables?

Overlapped density

  • 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-05-16T10:40:32+00:00Added an answer on May 16, 2026 at 10:40 am

    That image you linked to was for density curves, not histograms.

    If you’ve been reading on ggplot then maybe the only thing you’re missing is combining your two data frames into one long one.

    So, let’s start with something like what you have, two separate sets of data and combine them.

    carrots <- data.frame(length = rnorm(100000, 6, 2))
    cukes <- data.frame(length = rnorm(50000, 7, 2.5))
    
    # Now, combine your two dataframes into one.  
    # First make a new column in each that will be 
    # a variable to identify where they came from later.
    carrots$veg <- 'carrot'
    cukes$veg <- 'cuke'
    
    # and combine into your new data frame vegLengths
    vegLengths <- rbind(carrots, cukes)
    

    After that, which is unnecessary if your data is in long format already, you only need one line to make your plot.

    ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.2)
    

    enter image description here

    Now, if you really did want histograms the following will work. Note that you must change position from the default "stack" argument. You might miss that if you don’t really have an idea of what your data should look like. A higher alpha looks better there. Also note that I made it density histograms. It’s easy to remove the y = ..density.. to get it back to counts.

    ggplot(vegLengths, aes(length, fill = veg)) + 
       geom_histogram(alpha = 0.5, aes(y = ..density..), position = 'identity')
    

    enter image description here

    On additional thing, I commented on Dirk’s question that all of the arguments could simply be in the hist command. I was asked how that could be done. What follows produces exactly Dirk’s figure.

    set.seed(42)
    hist(rnorm(500,4), col=rgb(0,0,1,1/4), xlim=c(0,10))
    hist(rnorm(500,6), col=rgb(1,0,0,1/4), xlim=c(0,10), add = TRUE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 502k
  • Answers 502k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use DDay.iCal May 16, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer Looks like there is no proposed methodology to debug this… May 16, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer Assuming no special data structures or order, you can iterate… May 16, 2026 at 2:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have two data.frames in R, one of which has two columns and of
I have two SQL tables with data that I would like to compare. The
I'm using cython for a correlation calculation in my python program. I have two
I have some data in a dataframe calvarbyruno.1 with variables Nominal and PAR that
I have a data frame with 251 observations and 45 variables. There are 6
Alright. So I have a very large amount of binary data (let's say, 10GB)
I have written a code for joining two wave files.It works fine when i
I am profiling a C# application and it looks like two threads each calling
I have several .csv files with similar filenames except a numeric month (i.e. 03_data.csv,
I have fit a regression using lme4 thanks to a previous answer . Now

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.