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
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
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
Although the output has different colors, but I can't create the legends here.
Thanks in advance.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With