【问题标题】:Remove duplicates in two ggplot legend删除两个ggplot图例中的重复项
【发布时间】:2016-03-29 00:39:37
【问题描述】:

我在 R 中使用 ggplot2,并且有一个颜色(变量 1)和线型(变量 2)的手动刻度。两种类型的其中一个级别是相同的,我只是希望它出现在一条普通的行中,因此从 variable2 图例中消失。

请参阅下面的最少代码。

require(ggplot2)

data_0 <- expand.grid(x=1:2, 
    variable1=c("nothing", "A", "B"),
    variable2=c("nothing", "positif", "negatif") 
)
data <- subset(data_0, !((variable1=="nothing" & variable2 != "nothing") |
    (variable2=="nothing" & variable1 != "nothing")))
data$y <- rep(1:5, each = 2)

ggplot(data=data, aes(x=x, y=y, colour = variable1, lty = variable2))+
    geom_line(size=1.5)+
    theme_bw()+
    theme(legend.position="bottom")+
    scale_linetype_manual(values = c(1,3,5))

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    你们很亲密。您需要将breaks 指定为scale_linetype_manual

    library(ggplot2)
    
    ggplot(data=data, aes(x=x, y=y, colour = variable1, lty = variable2))+
      geom_line(size=1.5)+
      theme_bw()+
      theme(legend.position="bottom") +
      scale_linetype_manual(breaks = c("positif", "negatif"), values = c(1, 3, 5))
    

    【讨论】:

    • 我尝试过休息,但使用错误...我使用的是休息 = c(3,5)!非常感谢!
    猜你喜欢
    • 2020-07-08
    • 2015-06-08
    • 1970-01-01
    • 2018-11-03
    • 2016-06-07
    • 1970-01-01
    • 2016-04-09
    • 2021-12-07
    相关资源
    最近更新 更多