Adding a normalcy curve to a histogram?

Working through an assignment for my stats class, to add a normalcy curve (?) to a histogram. I can create the histogram, but when I type in the code exactly as it appears in the textbook I get an error message:

Here's the code (histsugars is the name of my histogram)
histsugars+stat_function(fun=dnorm, args=list(mean=mean(cereal$sugars, na.rm=TRUE, sd=sd(cereal$sugars, na.rm=TRUE) colour="black" size=1)

And here's the error message:

Error: unexpected symbol in "histsugars+stat_function(fun=dnorm, args=list(mean=mean(cereal$sugars, na.rm=TRUE, sd=sd(cereal$sugars, na.rm=TRUE) colour"

I have NO IDEA what the unexpected symbol is. I've added parenthesis, taken parenthesis away, triple-checked the code against what appears in my textbook, and I am stumped.

I count 4 opening brackets '(' but only 2 closing ')'

1 Like

So where do the closing brackets even go?? Is there an easy way to keep track of this?

Sorry, I'm VERY new to this & I find reading my code a little challenging.

Here's my new code. I still get an error message:

histsugars+stat_function(fun=dnorm, args=list(mean=mean(cereal$sugars, na.rm=TRUE), sd=sd(cereal$sugars, na.rm=TRUE)), colour="black" size=1)

You might need a comma between 'black' and size.

Generally I would advise trying to do the least, and then refining by adding in elements such as colouring after you have worked out the core maths and function of what you are trying to achieve. When learning something new over ambition and trying to implement too much at once can slow you down in the long run.

Okay, I tried: "histsugars+stat_function(fun=dnorm, args=list(mean=mean(cereal$sugars, na.rm=TRUE), sd=sd(cereal$sugars, na.rm=TRUE)), colour="black", size=1) and get one of two error messages:

  1. function stat_function not found or
  2. unexpected error

I just want a histogram with a line on it. This is supposed to be the code that gives me that, and I can't for the life of me figure out what I'm doing wrong.

(fwiw, I appreciate your suggestion about going minimal & I did originally try this with JUST the fun=dnorm and the sd part. But that code didn't add the line I wanted, so I thought I might need the rest of it, especially the color/size for the line. My textbook is only okay . . . it explains some parts of the code but skips over what some of the symbols and abbreviations mean, so it's hard to tell what's essential and what's not.)

Okay, I went back and made sure ggplot2 was loaded into my session, and now I have a line? So that's good, though the histogram itself does not show up for inexplicable reasons.

I have other things to do today than stats, so for reasons of time, I'm going to leave it here, but I do have a final wrap-up question: Do I have to load the packages I'll need into a session? I thought I was using ggplot2 yesterday (I was working thru a chapter on making graphs) but apparently I needed to reload this morning?

Fresh sessions typically won't have any packages/libraries loaded.
So it's common practice to have library() calls at the top of one's scripts, where you cover what you will need for the rest of your script.

Happy to help you. All the best.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.