Home > Software design >  How to add name labels to a graph using ggplot2 in R?
How to add name labels to a graph using ggplot2 in R?

Time:11-08

I have the following code:

plot <- ggplot(data = df_sm)  
  geom_histogram(aes(x=simul_means, y=..density..), binwidth = 0.20, fill="slategray3", col="black", show.legend = TRUE)
plot <- plot   labs(title="Density of 40 Means from Exponential Distribution", x="Mean of 40 Exponential Distributions", y="Density")
plot <- plot   geom_vline(xintercept=sampl_mean,size=1.0, color="black", show.legend = TRUE)
plot <- plot   stat_function(fun=dnorm,args=list(mean=sampl_mean, sd=sampl_sd),color = "dodgerblue4", size = 1.0)
plot <- plot  geom_vline(xintercept=th_mean,size=1.0,color="indianred4",linetype = "longdash")
plot <- plot   stat_function(fun=dnorm,args=list(mean=th_mean, sd=th_mean_sd),color = "darkmagenta", size = 1.0)
plot 

I want to show the legends of each layer, I tried show.legend = TRUE but it does nothing.

All my data frame is means from exponential distribution simulations, also I have some theoretical values from the distribution (mean and standard deviation) which I describe as th_mean and th_mean_sd.

The code for my simulation is the following:

lambda <- 0.2
th_mean <- 1/lambda
th_sd <- 1/lambda
th_var <- th_sd^2
n <- 40
th_mean_sd <- th_sd/sqrt(n)
th_mean_var <- th_var/sqrt(n)
simul <- 1000

simul_means <- NULL
for(i in 1:simul) {
  simul_means <- c(simul_means, mean(rexp(n, lambda)))
}

sampl_mean <- mean(simul_means)
sampl_sd <- sd(simul_means)

df_sm<-data.frame(simul_means)

CodePudding user response:

If you want to get a legend you have to map on aesthetics instead of setting the color, fill, ... as parameter, i.e. move color=... inside aes(...) and make use of scale_color/fill_manual to set the color values. Personally I find it helpful to make use of some meaningful labels, e.g. in case of your histogram I map the label "hist" on the fill but you could whatever label you like:

set.seed(123)

lambda <- 0.2
th_mean <- 1 / lambda
th_sd <- 1 / lambda
th_var <- th_sd^2
n <- 40
th_mean_sd <- th_sd / sqrt(n)
th_mean_var <- th_var / sqrt(n)
simul <- 1000

simul_means <- NULL
for (i in 1:simul) {
  simul_means <- c(simul_means, mean(rexp(n, lambda)))
}

sampl_mean <- mean(simul_means)
sampl_sd <- sd(simul_means)

df_sm <- data.frame(simul_means)

library(ggplot2)

ggplot(data = df_sm)  
  geom_histogram(aes(x = simul_means, y = ..density.., fill = "hist"), binwidth = 0.20, col = "black")   
  labs(title = "Density of 40 Means from Exponential Distribution", x = "Mean of 40 Exponential Distributions", y = "Density")   
  stat_function(fun = dnorm, args = list(mean = sampl_mean, sd = sampl_sd), aes(color = "sampl_mean"), size = 1.0)   
  stat_function(fun = dnorm, args = list(mean = th_mean, sd = th_mean_sd), aes(color = "th_dens"), size = 1.0)  
  geom_vline(size = 1.0, aes(xintercept = sampl_mean, color = "sampl_mean"))   
  geom_vline(size = 1.0, aes(xintercept = th_mean, color = "th_mean"), linetype = "longdash")   
  scale_fill_manual(values = c(hist = "slategray3"))  
  scale_color_manual(values = c(sampl_dens = "dodgerblue4", th_dens = "darkmagenta", th_mean = "indianred4", sampl_mean = "black"))

  • Related