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

  • SEARCH
  • Home
  • 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 6372777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:16:47+00:00 2026-05-25T01:16:47+00:00

I have question probably similar to Fitting a density curve to a histogram in

  • 0

I have question probably similar to Fitting a density curve to a histogram in R. Using qplot I have created 7 histograms with this command:

 (qplot(V1, data=data, binwidth=10, facets=V2~.)   

For each slice, I would like to add a fitting gaussian curve. When I try to use lines() method, I get error:

Error in plot.xy(xy.coords(x, y), type = type, ...) : 
plot.new has not been called yet

What is the command to do it correctly?

  • 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-25T01:16:48+00:00Added an answer on May 25, 2026 at 1:16 am

    Have you tried stat_function?

    + stat_function(fun = dnorm)
    

    You’ll probably want to plot the histograms using aes(y = ..density..) in order to plot the density values rather than the counts.

    A lot of useful information can be found in this question, including some advice on plotting different normal curves on different facets.

    Here are some examples:

    dat <- data.frame(x = c(rnorm(100),rnorm(100,2,0.5)), 
                      a = rep(letters[1:2],each = 100))
    

    Overlay a single normal density on each facet:

    ggplot(data = dat,aes(x = x)) + 
      facet_wrap(~a) + 
        geom_histogram(aes(y = ..density..)) + 
        stat_function(fun = dnorm, colour = "red")
    

    enter image description here

    From the question I linked to, create a separate data frame with the different normal curves:

    grid <- with(dat, seq(min(x), max(x), length = 100))
    normaldens <- ddply(dat, "a", function(df) {
      data.frame( 
        predicted = grid,
        density = dnorm(grid, mean(df$x), sd(df$x))
      )
    })
    

    And plot them separately using geom_line:

    ggplot(data = dat,aes(x = x)) + 
        facet_wrap(~a) + 
        geom_histogram(aes(y = ..density..)) + 
        geom_line(data = normaldens, aes(x = predicted, y = density), colour = "red")
    

    enter image description here

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

Sidebar

Related Questions

This is probably a very trivial question... I have a table similar to this:
Ok, so this question probably isn't Silverlight specific. I have a silverlight 2 page
I realize this question has probably been asked numerous times, but I have not
This is confusing me, so this question will probably be confusing. I have a
This is probably more a design or style question: I have just been considering
This is probably a simple question, but i have been unable to find a
This is probably an easy question... I have 4 source versions of the same
This is probably a silly question, but here it goes.Imagine you have the following
This is probably a basic html/css question... I have a simple one-button form that
This question hasn't been asked yet. Some solutions probably have been proposed though to

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.