在 ggplot2 中创建图例和添加图

use*_*r03 5 r ggplot2

我想使用ggplot2不同的颜色和图例在一个图中创建多个核密度图。我是通过以下方式完成的:

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")
Run Code Online (Sandbox Code Playgroud)

输出

在此处输入图片说明

当要比较的数字带宽超过 6 时,时间相当长。所以,我使用 for 循环来做同样的事情。

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
Run Code Online (Sandbox Code Playgroud)

阴谋

在此处输入图片说明

但在这里,在 for 循环下创建的图的颜色保持不变。

我也试着保持color在外面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
Run Code Online (Sandbox Code Playgroud)

阴谋

在此处输入图片说明

虽然输出有不同的颜色,但我不能在这里创建图例。

提前致谢。

use*_*748 4

我会这样构建它:

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")
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020-04-09 创建