I've got an dataframe df
with 50k rows and 6 columns. Now I plotted all 6 columns in 6 subplots thanks to a solution I could find on here:
library(ggplot2)
library(tidyr)
ggplot(gather(df, cols, value), aes(x = value)) +
geom_histogram(binwidth = 0.25) +
facet_wrap(.~cols)
Background: For all data columns I've drawn random numbers between 0 and 10. For every column I drew more numbers and calculated the mean. As I did this 50k times and plotted the numbers on the histograms. The first subplot is almost plain, and the last subplot looks like a skyscraper.
Now of course I found several examples how to add a nd curve to a histogram, but those were all without subplots, so I can't get it working. My new code (source):
ggplot(gather(df, cols, value), aes(x = value)) +
geom_histogram(binwidth = 0.25) +
stat_function(fun = dnorm, args = list(mean = mean(df$n1), sd = sd(df$n1))) +
facet_wrap(.~cols)
As you can see, I try to get the mean and sd from my first data column (they are named n1, n2, n3, n10, n100, n1000 for the number of drawings). So my problems are those:
- The code doesn't work for now as the curve is just plain zero in every subplot. What did I do wrong?
- How do I use a different means and sd for every subplot?
Thank you for any help!
edit:
My df
gets generated like this:
ROWS = 50000
MIN = 0
MAX = 10
df = data.frame(n1 = replicate(ROWS, mean(runif(n = 1, min = MIN, max = MAX))))
df$n2 = replicate(ROWS, mean(runif(n = 2, min = MIN, max = MAX)))
df$n3 = replicate(ROWS, mean(runif(n = 3, min = MIN, max = MAX)))
df$n10 = replicate(ROWS, mean(runif(n = 10, min = MIN, max = MAX)))
df$n100 = replicate(ROWS, mean(runif(n = 100, min = MIN, max = MAX)))
df$n1000 = replicate(ROWS, mean(runif(n = 1000, min = MIN, max = MAX)))