geom_freqpoly not plotting when I run this chunk of code with ggplot2

Viewed 148

I don't know why geom_freqpoly is not graphing. When I run the chunk I only see the histogram but not the line graph.

library(ggplot2)
lambda1 = 1/2
sequence = seq(1, 10000, by=1)
    

df1 = tibble(x=seq(1, 10000, by=1), expo1 = lambda1*exp(-(lambda1)*x))

lambda2 = 1/5

df2 = tibble(x=seq(1, 10000, by=1), expo2 = lambda2*exp(-(lambda2)*x))


ggplot() +
  geom_histogram(aes(x=expo2, y=..density..), binwidth=.5, colour="blue", fill="white") +
  geom_histogram(aes(x=expo1, y=..density..), binwidth=.5, colour="red", fill="white") +
  geom_freqpoly(data=df2, aes(x=expo2, y=..density..), binwidth=.5, colour="blue") + 
  geom_vline(aes(xintercept=mean(expo2)), color="blue", linetype="dashed", size=1) +
  geom_density(alpha=.2, fill="#FF6666") +
  geom_freqpoly(data=df1, aes(x=expo1, y=..density..), binwidth=.5, colour="red") + 
  geom_vline(aes(xintercept=mean(expo1)), color="red", linetype="dashed", size=1) +
  geom_density(alpha=.2, fill="#FF6666") +
  xlim(0,10) +
  ylim(0,0.5)+
  ggtitle("Distribution of averages of random exponential distribution with their means", "theta = 2 in red and theta = 5 in ")+
  xlab("Averages of the distribution")+
  ylab("Density")
1 Answers

You are missing the data object in many of your geom lines. Each geom needs data. What are you trying to do? Plot two dataframes on one plot? Your scales are all over the place. Try this.


library(ggplot2)
library(tibble)

x <- seq(0, 10, by = 0.1)

lambda1 <- 1 / 2
df1 <- tibble(x = x, expo1 = lambda1 * exp(-(lambda1) * x))

lambda2 <- 1 / 5
df2 <- tibble(x = x, expo2 = lambda2 * exp(-(lambda2) * x))

ggplot() +
  ggtitle("Distribution of averages of random exponential distribution with their means", "theta = 2 in red and theta = 5 in blue") +
  xlab("Averages of the distribution") +
  ylab("Density") +
  geom_histogram(data = df2, aes(x = x, y = expo2), stat = "identity", colour = "blue", fill = "white") +
  geom_freqpoly(data = df2, aes(x = x, y = expo2), stat = "identity", colour = "blue") +
  geom_vline(xintercept = lambda2, color = "blue", linetype = "dashed", size = 1) +
  geom_density(data = df2, aes(x = x, y = expo2), stat = "identity", alpha = .2, fill = "#FF6666") +
  geom_histogram(data = df1, aes(x = x, y = expo1), stat = "identity", colour = "red", fill = "white") +
  geom_freqpoly(data = df1, aes(x = x, y = expo1), stat = "identity", colour = "red") +
  geom_vline(xintercept = lambda1, color = "red", linetype = "dashed", size = 1) +
  geom_density(data = df1, aes(x = x, y = expo1), stat = "identity", alpha = .2, fill = "#FF6666")
#> Warning: Ignoring unknown parameters: binwidth, bins, pad

#> Warning: Ignoring unknown parameters: binwidth, bins, pad

Created on 2020-11-02 by the reprex package (v0.3.0)

Related