Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2使用不同几何图形时,将图例键从十字切换到直线_R_Ggplot2_Tidyverse - Fatal编程技术网

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