【问题标题】:How to change angle of line in customized legend in ggplot2如何在ggplot2中更改自定义图例中的线条角度
【发布时间】:2016-06-12 18:05:21
【问题描述】:

我正在尝试向我的 ggplot 添加自定义图例,但图例框的线条呈一定角度。我想将该角度更改为 0 度。有没有办法做到这一点?以下是示例图的代码。

ggplot()+geom_abline(aes(color="black",,slope=1,intercept = 0))+
   geom_abline(aes(color="red",slope=0.5,intercept = 0))+
   scale_color_manual(values=c("black"="black","red"="red"))

我们可以看到图例框中的线条略微倾斜,我想让它们水平。

【问题讨论】:

  • 使用geom_line 代替geom_abline 并根据截距和斜率计算点对。
  • @Roland 我可以这样做,但我正在从各种回归中绘制多条线,所以我试图弄清楚是否有任何方便的功能可以利用,比如 override.aes 或其他东西否则..

标签: r ggplot2 legend-properties


【解决方案1】:

您可以更改键中线条的绘制方式:我更改了segmentsGrob 的y0 和y1,使它们位于中心(=0.5)。 (ps在更改之前先看看GeomAbline$draw_key)

library(ggplot2)
library(grid)

GeomAbline$draw_key <- function(data, params, size) 
{
    segmentsGrob(0, 0.5, 1, 0.5, gp = gpar(col = alpha(data$colour, 
        data$alpha), lwd = data$size * .pt, lty = data$linetype, 
        lineend = "butt"))
}  

ggplot() + geom_abline(aes(color="black",slope=1,intercept = 0))+
   geom_abline(aes(color="red",slope=0.5,intercept = 0))+
   scale_color_manual(values=c("black"="black","red"="red"))

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多