如何在图例中显示geom_abline的abline [英] How to show abline of geom_abline in legend

查看:647
本文介绍了如何在图例中显示geom_abline的abline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例数据中,我如何使用y在图例中显示abline(即红色线)。
我的代码和数据:

  x <-c(1990,1991,1992,1993,1994,1995) 
y< -c(400,500,465,450,550,555)

df <-data.frame(x,y)
df
plot1 < - ggplot(df,aes(x) )+
geom_line(size = 0.5,lty =dashed,aes(y = y),color =Blue)+
geom_abline(aes(斜率= -0.62,截距= 1670,color =break),size = 0.9)+
geom_point(aes(y = y,shape =y),size = 4,color =Gray24,fill =Green)
plot1


我得到的是下面的

解决方案

您可以使用 show_guide = TRUE

code>参数:

  plot1 < -  ggplot(df,aes(x))+ 
geom_line aes(y = y),color =Blue)+
geom_abline(aes(slope = -0.62,截距= 1670,color =break),size = 0.9,show_guide = TRUE)+
geom_point(aes(y = y,shape =y),size = 4,color =Gray24,fill =Green)

您可能需要更改图例中的标签,但您应该可以使用主题编辑:要从图例中删除斜线,您可以使用 guides 。。
>和 override.aes

  plot1 < -  ggplot(df, aes(x,y))+ 
geom_point(aes(shape =y),size = 4,color =Gray24,lty = 0)+
geom_line(size = 0.5, dashed,color =Blue)+
geom_abline(aes(slope = -0.62,截距= 1670,color =break),s ize = 0.9,
show_guide = TRUE)+
guides(shape = guide_legend(override.aes = list(linetype = 0)))


in the following sample data , how can i display the abline ( i e the red color line) in legend with y. my code and data:

x<-c(1990,1991,1992,1993,1994,1995)
y<-c(400,500,465,450,550,555)

df<-data.frame(x,y)
df
plot1<- ggplot(df, aes(x)) +
        geom_line(size=0.5,lty="dashed", aes(y=y),color="Blue") +
        geom_abline(aes(slope=-0.62,intercept=1670,colour="break"),size=0.9)+
        geom_point(aes(y=y,shape="y"),size=4, color="Gray24",fill="Green")
plot1

. what i got is the below image. i need the red line to be displayed in legend

解决方案

You can use the show_guide=TRUE argument:

plot1<- ggplot(df, aes(x)) +
  geom_line(size=0.5,lty="dashed", aes(y=y),color="Blue") +
  geom_abline(aes(slope=-0.62,intercept=1670,colour="break"), size=0.9,show_guide = TRUE)+
  geom_point(aes(y=y,shape="y"),size=4, color="Gray24",fill="Green")

You'll probably need to change the labels in the legend, but you should be able to do that with theme.

Edit: To remove the slash from the legend, you can use guides and override.aes:

plot1 <- ggplot(df, aes(x, y)) +
  geom_point(aes(shape = "y"), size = 4, color = "Gray24", lty = 0) +
  geom_line(size = 0.5, lty = "dashed", color = "Blue") +
  geom_abline(aes(slope = -0.62, intercept = 1670, colour = "break"), size = 0.9, 
               show_guide = TRUE) +
  guides(shape = guide_legend(override.aes = list(linetype = 0)))

这篇关于如何在图例中显示geom_abline的abline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆