更改 geom_density 中的图例
Changing legend in geom_density
我不明白为什么图例在给定以下代码的情况下没有改变。 geom_histogram 的相同选项也有同样的作用。在此先感谢您的帮助。
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_fill_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))
您在 aes()
的调用中使用了 color
。要修改此变量的比例,您需要使用 scale_color_manual
而不是 scale_fill_manual
.
这很棘手,因为 geom_histogram
确实使用了 fill
,但是 geom_density
使用了 color
。
工作解决方案:
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_color_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))
我不明白为什么图例在给定以下代码的情况下没有改变。 geom_histogram 的相同选项也有同样的作用。在此先感谢您的帮助。
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_fill_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))
您在 aes()
的调用中使用了 color
。要修改此变量的比例,您需要使用 scale_color_manual
而不是 scale_fill_manual
.
这很棘手,因为 geom_histogram
确实使用了 fill
,但是 geom_density
使用了 color
。
工作解决方案:
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_color_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))