gpt4 book ai didi

r - 在 ggplot2 中创建图例和添加图

转载 作者:行者123 更新时间:2023-12-03 16:35:46 28 4
gpt4 key购买 nike

我想使用 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")

output

enter image description here

当要比较的数字带宽超过 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

plot

enter image description here

但在这里,在 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

plot

enter image description here

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

提前致谢。

最佳答案

我会像这样构造它:

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")



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

关于r - 在 ggplot2 中创建图例和添加图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61128343/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com