2015-12-22 4 views
6

में डुप्लिकेट निकालें मैं आर में ggplot2 का उपयोग कर रहा हूं और रंग (चर 1) और लाइन प्रकार (variable2) के लिए मैन्युअल स्केल है। स्तरों में से एक दोनों प्रकार के लिए समान है और मैं इसे एक सादा रेखा में दिखाना पसंद करूंगा और इसलिए परिवर्तनीय 2 किंवदंती से गायब हो जाऊंगा।दो ggplot legend

नीचे न्यूनतम कोड देखें।

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

तुम बहुत करीब थे। मैं टूट जाता है की कोशिश की

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

(3,5) और गलत उपयोग कर रहा था ... मैं टूट जाता है = ग उपयोग कर रहा था: आप breaksscale_linetype_manual को निर्दिष्ट करने के लिए की जरूरत है! बहुत धन्यवाद! – PerrySun