0

I want the distribution f to show up in my plot.

library(ggplot2)
f <- dnorm(x=2)
x <- c(v6$Zwischenankunftszeit)
counts <- c(v6$ZugangFolgeVrg)
df <- data.frame(x=x, counts=counts)

plt <- ggplot(df + f) + geom_bar(aes(x=x, y=counts), stat="identity")
print(plt)
Medicat
  • 15
  • 5
  • what is `v6` variable? Are you sure this code works for you? – mnm May 26 '20 at 13:05
  • v6 ist the name of my Dataset this works, but only shows the df plot – Medicat May 26 '20 at 13:12
  • 1
    See this post and learn [how to ask a Q](https://stackoverflow.com/help/how-to-ask). and how to create a [minimal reprex](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) Then come back here and revise your Q, else there is high risk of it being closed or down-voted! – mnm May 26 '20 at 13:17

1 Answers1

0

If you want to plot a histogram with a normal distribution on top, this is one of the easier ways:

library(ggplot2)

df <- data.frame(
  x = rnorm(100, mean = 50, sd = 5)
)

ggplot(df, aes(x)) +
  geom_histogram(aes(y = stat(density))) +
  stat_function(fun = dnorm, args = list(mean = mean(df$x), sd = sd(df$x)))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Created on 2020-05-26 by the reprex package (v0.3.0)

If you want to keep the y-axis in count units, you'd have to wrap the dnorm function and scale it by the binwidth and number of observations:

wrap_dnorm <- function(x, mean = 0, sd = 1, binwidth = 1, count = 1) {
  dnorm(x, mean = mean, sd = sd) * binwidth * count
}

ggplot(df, aes(x)) +
  geom_histogram(binwidth = 1) +
  stat_function(fun = wrap_dnorm, 
                args = list(mean = mean(df$x), sd = sd(df$x), 
                            binwidth = 1, count = nrow(df)))
teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Thank you very much. The remaining Problems is, that the data for the histogram are two vectors. `x<-c(0,2,4,5,10,30,50) and count<-c(30,55,27,94,1,34,5)` x is my value, count its quantity – Medicat May 27 '20 at 06:28