Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Legends and adding plots in ggplot2

Tags:

I want to create multiple plots of kernel density plots in one plot using ggplot2 with different colors and legend. That I have done in the following way:

library(tidyverse)
set.seed(1234)
x <- rnorm(25)
x %>% tibble() %>% ggplot(aes(x = values)) +
  stat_density(aes(x, color = "0.1"), position = "identity", geom = "line", 
               kernel = "gaussian", bw = 0.1) +
  stat_density(aes(x, color = "0.2236"), position = "identity", geom = "line", 
               kernel = "gaussian", bw = 0.2236) +
  stat_density(aes(x, color = "0.334"), position = "identity", geom = "line", 
               kernel = "gaussian", bw = 0.334) +
  stat_density(aes(x, color = "0.578"), position = "identity", geom = "line", 
               kernel = "gaussian", bw = 0.578) +
  stat_density(aes(x, color = "0.7"), position = "identity", geom = "line", 
               kernel = "gaussian", bw = 0.7) +
  stat_density(aes(x, color = "1.0"), position = "identity", geom = "line", 
               kernel = "gaussian", bw = 1.0) +
  stat_density(aes(x, color = "2"), position = "identity", geom = "line", 
               kernel = "gaussian", bw = 2) +
  stat_density(aes(x, color = "3.5"), position = "identity", geom = "line", 
               kernel = "gaussian", bw = 3.5) +
  stat_density(aes(x, color = "5"), position = "identity", geom = "line", 
               kernel = "gaussian", bw = 5) +
  stat_function(fun = function(y) dnorm(y), aes(x, color = "True density")) +
  labs( title = "Different Choice of Bandwidth", color = "Bandwidth")

output

enter image description here

It is quite lengthy when the number bandwidth to be compared is more than 6. So, I used for loop to do the same .

bw_choice <- c(  0.1, 0.2236, 0.334, 0.578,
                    0.7, 1.0, 2, 3.5, 5)
plot <- x %>% tibble() %>% ggplot(aes(x = values))
for (i in 1:length(bw_choice)) {
  plot <-  plot + stat_density(aes(x , color = as.character(bw_choice[i])),
                                   position = "identity", geom = "line", 
                                   kernel = "gaussian", 
                                   bw = bw_choice[i])
}
plot <- plot + stat_function(fun = function(y) dnorm(y), aes(x, color = "True Density")) + 
  labs( title = "Different Choice of Bandwidth", color = "Bandwidth")
plot

plot

enter image description here

But here, the colors of the plots that are created under for loop remains same.

I also tried keeping color outside aes.

color <- c( "#FFCC00", "#FF3300", "#99CC00", "#CC0033", "#666600", "#FF3399", "#3300CC", 
              "#33FFCC", "#003300", "#003366")
bw_choice <- c(  0.1, 0.2236, 0.334, 0.578,
                    0.7, 1.0, 2, 3.5, 5)
plot <- x %>% tibble() %>% ggplot(aes(x = values))
for (i in 1:length(bw_choice)) {
  plot <-  plot + stat_density(aes(x), colour = color[i] ,
                                   position = "identity", geom = "line", 
                                   kernel = "gaussian", 
                                   bw = bw_choice[i])
}
plot <- plot + stat_function(fun = function(y) dnorm(y), aes(x), color = color[10]) + 
  scale_color_manual(values = c( '#FFCC00', "#FF3300", "#99CC00", "#CC0033", 
                                 "#666600", "#FF3399", "#3300CC",
                                 "#33FFCC", "#003300", "#003366"), name = "Bandwidth",
                     labels = c("0.1", "0.2236", "0.334", "0.578",
                                "0.7", "1.0", "2", "3.5", "5", "Density"))+
  labs( title = "Different Choice of Bandwidth", color = "Bandwidth")
plot

plot

enter image description here

Although the output has different colors, but I can't create the legends here.

Thanks in advance.

like image 443
user03 Avatar asked Apr 09 '20 18:04

user03


1 Answers

I would construct it like this:

library(tidyverse)
set.seed(1234)
x <- rnorm(25)
bw_choice <- c(  0.1, 0.2236, 0.334, 0.578,
                 0.7, 1.0, 2, 3.5, 5)
sts <- lapply(seq_along(bw_choice), function(i) stat_density(data=tibble(x), aes(x , colour = as.character(bw_choice[i])),
             position = "identity", geom = "line", 
             kernel = "gaussian", 
             bw = bw_choice[i]))
ggplot(tibble(x), aes(x = values)) +
    sts +
    stat_function(fun = function(y) dnorm(y), aes(x, color = "True Density")) + 
    labs( title = "Different Choice of Bandwidth", color = "Bandwidth")

Created on 2020-04-09 by the reprex package (v0.3.0)

like image 158
user12728748 Avatar answered Oct 17 '22 00:10

user12728748