2016-05-23 9 views
2

Ich erstelle 4 Zeile in einem Diagramm mit R. Hier ist der CodeHinzufügen einer Legende zu einem Plot mit R

# Define 2 vectors 
cars <- c(123.07, 110.51, 96.14, 98.71, 101.3) 
trucks <- c(110.31, 89.91, 89.81, 89.31, 93.4, 95.81) 
cars1 <- c(123.227, 110.221, 93.14, 98.22, 122.3) 
trucks1 <- c(120.31, 89.91, 89.81, 89.31, 93.4, 95.81) 
# Graph cars using a y axis that ranges from 0 to 12 
plot(cars, type="o", col="blue", ylim=c(80,130)) 

# Graph trucks with red dashed line and square points 
lines(trucks, type="o", pch=22, lty=2, col="red") 
lines(cars1, type="o", col="yellow", ylim=c(80,130)) 

# Graph trucks with red dashed line and square points 
lines(trucks1, type="o", pch=22, lty=2, col="green") 
# Create a title with a red, bold/italic font 
title(main="Autos", col.main="red", font.main=4) 

Ich mag würde wissen, wie kann ich die Legende für jede Parzelle (Linie) im Plot hinzuzufügen.

Danke

Antwort

3
legend("topleft", c("cars", "trucks", "cars1", "trucks1"), 
text.col = "blue", "red", "yellow", "green") 

Verwenden Sie das? Legende Befehlsoptionen, wie andere Stellen auf dem Diagramm neben „topleft“ oder die Textgröße zu sehen, ob ein Feld um die Legende zu haben usw.

1

Zum Beispiel

legend(
    "topright", 
    lty=c(1,2,1,2), 
    col=c("blue", "red", "yellow", "green"), 
    legend = c("cars", "trucks", "cars1", "trucks1") 
) 

?legend See.

Verwandte Themen