在两个 ggplot 图例中删除重复项

6
我正在使用R中的ggplot2,手动设置了颜色(variable1)和线型(variable2)的比例尺。其中一个级别适用于两种类型,并且我只希望它以普通线条的形式出现,因此从variable2图例中消失。
请参见下面的最小代码。 enter image description here
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))
1个回答

11
你非常接近了。你需要在 scale_linetype_manual 中指定 breaks 参数:
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))

在此输入图像描述


2
我尝试使用breaks,但是用错了...我使用的是breaks = c(3,5)! 非常感谢! - PerrySun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接