R ggplot2使用不同几何图形时,将图例键从十字切换到直线
我有这个密码R ggplot2使用不同几何图形时,将图例键从十字切换到直线,r,ggplot2,tidyverse,R,Ggplot2,Tidyverse,我有这个密码 library(tidyverse) df=mammals ggplot(df, aes(log(body))) + stat_bin(aes(y = ..density..)) + stat_function(fun = "dnorm", aes(color = "a", linetype = "a")) + geom_vline(aes
library(tidyverse)
df=mammals
ggplot(df, aes(log(body))) +
stat_bin(aes(y = ..density..)) +
stat_function(fun = "dnorm",
aes(color = "a",
linetype = "a")) +
geom_vline(aes(
xintercept = -4,
color = "b",
linetype = "b"
)) +
geom_vline(aes(
xintercept = -3,
color = "c",
linetype = "b"
)) +
guides(linetype = FALSE)
它生成了这个图:
然而,我不希望有十字架。我想要三条线。在最好的情况下,它们将是实心和虚线。我尝试了这一点,但只取得了部分成功(我也不确定为什么我必须在覆盖.aes
中放置“b”
两次……:
但是,我不知道如何将图例符号更改为线条
将
show.legend=FALSE
中的stat\u function()
添加到绘图中是否可以执行您想要的操作,或者您想要水平线而不是垂直线?有关更改图例中的图示符的信息,请参见不同几何图形中的键图示符。我总是要检查上有哪些符号可用?draw_key
是!!:)非常感谢你!一个简单的show.legend=FALSE
就成功了
library(tidyverse)
df = mammals
ggplot(df, aes(log(body))) +
stat_bin(aes(y = ..density..)) +
stat_function(fun = "dnorm",
aes(color = "a",
linetype = "a")) +
geom_vline(aes(
xintercept = -4,
color = "b",
linetype = "b"
)) +
geom_vline(aes(
xintercept = -3,
color = "c",
linetype = "b"
)) +
guides(linetype = FALSE, color = guide_legend(override.aes = list(
linetype = c("a" = "dashed", "b" = "solid", "b" = "solid")
)))