Kok Lin
Kok Lin

Reputation: 39

How to plot the probability density function graph for multiple data set in 1 graph?

enter image description here

I have generated 100 samples for Poisson Distribution with sample size n = 100, lambda = 3. Now I'm trying to plot the probability density function graph for all 100 samples in the same graph.

set.seed(124)


Sample100 <- replicate(100, matrix(data=rpois(100, lambda = 3),ncol = 1), simplify = TRUE)

View(Sample100)

colMeans(Sample100)
apply(Sample100, 2, var)

plot(density(Sample100),xlab = "y", ylab = "P(Y)",main = "Probability density function for 100 sample" )

What I get from the code is that it just providing me 1 line supposing I will get 100 lines in the graph because I'm having 100 sets of data

Upvotes: 1

Views: 590

Answers (1)

Justin Landis
Justin Landis

Reputation: 2071

if you are familiar with ggplot you can do the following

set.seed(124)
library(ggplot2)
library(tidyr)
Sample100 <- replicate(100, matrix(data=rpois(100, lambda = 3),ncol = 1), simplify = TRUE)
Samplelong <- gather(as.data.frame(Sample100))

ggplot(Samplelong, aes(x = value)) +
       geom_density(aes(group = key)) +
       labs(x = "y", #labs function overides labels on the plot
            y = "P(y)",
            title = "Probability density function for 100 sample") +
       theme_classic() +   # This changes the theme to look similar to base r plot theme
       #theme function allows you to modify plot further, everyting from size
       # fonts, angles, axis.
       theme(plot.title = element_text(hjust = 0.5, #hjust - horizontal shift
                                       face = "bold")) + . # face - the print for plot.title is set to bold
       scale_x_continuous(expand = c(0,0))  + #for the continuous x axis, limit expansion to 0 on both ends
       scale_y_continuous(expand = c(0,0)) #for the continuous y axis, limit expansion to 0 on both ends

for Reference you were plotting the following it seems

ggplot(Samplelong, aes(x = value)) +
       geom_density() +
       labs(x = "y", y = "P(y)", title = "Probability density function for 100 sample") +
       theme_classic() +
       theme(plot.title = element_text(hjust = 0.5)) +
       scale_x_continuous(expand = c(0,0))  +
       scale_y_continuous(expand = c(0,0))

if ggplot2 confuses you, just take you time with trying to make the graph pretty, for most purposes the following is fine.

 ggplot(Samplelong, aes(x = value)) +
   geom_density(aes(group = key)) +
   labs(x = "y", #labs function overides labels on the plot
        y = "P(y)",
        title = "Probability density function for 100 sample")

Upvotes: 1

Related Questions