如何在不更改 ggplot2 中的图例的情况下更改密度曲线?
How to change a density curve without changing the legend in ggplot2?
我想更改密度曲线的颜色,但是当我更改它时,图例中的框的边框也会更改。
原剧情:
当我改变密度曲线的颜色时:
此外,如果我从密度曲线中添加 lwd=1.2
,图例也会发生变化。
有人知道怎么解决吗?
(我想更改线宽和密度曲线的颜色,但我不想更改图例的外观)。
这是代码:
val1 <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
val2 <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df1 <- data.frame(value = val1)
df2 <- data.frame(value = val2)
data <- bind_rows(lst(df1, df2), .id = 'id')
data %>%
ggplot(aes(value)) +
geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.4) +
geom_density(lwd = 1.2,
colour = "red") +
facet_grid(id ~ .) +
scale_x_continuous(breaks=pretty(data$value, n=10)) +
ggtitle("My histogram....") +
guides(fill=guide_legend(title="My legend...")) +
theme(strip.text.x = element_blank(),strip.text.y = element_blank())
非常感谢!
此致
将show.legend = FALSE
添加到geom_density()
library(ggplot2)
library(dplyr)
data %>%
ggplot(aes(value)) +
geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.4) +
geom_density(lwd = 1.2, colour = "red", show.legend = FALSE) +
facet_grid(id ~ .) +
scale_x_continuous(breaks=pretty(data$value, n=10)) +
ggtitle("My histogram....") +
guides(fill = guide_legend(title="My legend...")) +
theme(strip.text.x = element_blank(),strip.text.y = element_blank())
由 reprex package (v2.0.1)
于 2021-12-17 创建
我想更改密度曲线的颜色,但是当我更改它时,图例中的框的边框也会更改。
原剧情:
当我改变密度曲线的颜色时:
此外,如果我从密度曲线中添加 lwd=1.2
,图例也会发生变化。
有人知道怎么解决吗? (我想更改线宽和密度曲线的颜色,但我不想更改图例的外观)。
这是代码:
val1 <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
val2 <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df1 <- data.frame(value = val1)
df2 <- data.frame(value = val2)
data <- bind_rows(lst(df1, df2), .id = 'id')
data %>%
ggplot(aes(value)) +
geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.4) +
geom_density(lwd = 1.2,
colour = "red") +
facet_grid(id ~ .) +
scale_x_continuous(breaks=pretty(data$value, n=10)) +
ggtitle("My histogram....") +
guides(fill=guide_legend(title="My legend...")) +
theme(strip.text.x = element_blank(),strip.text.y = element_blank())
非常感谢!
此致
将show.legend = FALSE
添加到geom_density()
library(ggplot2)
library(dplyr)
data %>%
ggplot(aes(value)) +
geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.4) +
geom_density(lwd = 1.2, colour = "red", show.legend = FALSE) +
facet_grid(id ~ .) +
scale_x_continuous(breaks=pretty(data$value, n=10)) +
ggtitle("My histogram....") +
guides(fill = guide_legend(title="My legend...")) +
theme(strip.text.x = element_blank(),strip.text.y = element_blank())
由 reprex package (v2.0.1)
于 2021-12-17 创建