2015-12-22 25 views
6

私はRでggplot2を使用していて、カラー(variable1)とラインタイプ(variable2)にはマニュアルスケールがあります。レベルの1つは両方のタイプで同じで、単純な線で表示したいので、variable2の凡例から消えてしまいます。ggplotの2つの凡例で重複を削除します

下記の最小限のコードを参照してください。

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

答えて

11

あなたは非常に近かったです。 (3,5)、私は休憩= cを使用していた...私は休憩を試してみました

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

enter image description here

+2

、それは間違って使用していた:あなたはbreaksscale_linetype_manualに指定する必要があります!どうもありがとう! – PerrySun

関連する問題