2013-10-14 187 views
2

我一直在爲pairs()工作,因此我花費了一段令人尷尬的漫長時間嘗試添加傳奇或使用幻影符號來改變標題中物種的顏色以與點相關...任何見解都將不勝感激!這是個問題。如何將自定義圖例添加到R中的圖形?

# original code 
pairs(iris[1:4], 
    main = "Width and Length (cm) of Iris's Petals and Sepals: Setosa, Veriscolor, Virginica", 
    pch = 24, bg = c("mediumorchid4", "mediumpurple1", "lightpink1")[unclass(iris$Species)], 
    upper.panel=NULL, labels=c("Sepal Length","Sepal Width","Petal Length","Petal Width"), 
    font.labels=1, cex.labels=2) 
+5

請參閱'?legend'。爲了正確定位,你可能需要使用'locator()'來獲取(x,y)座標(右鍵單擊以將其轉義並返回到命令行)。然後使用返回的座標來放置圖例,例如'legend(x = 0.7,y = 0.8,...)' –

回答

5

這裏有一個更好的選擇,公正的情況下(不太好)接受的答案消失:

pairs(iris[1:4], 
    main = "Width and Length (cm) of Iris's Petals and Sepals: Setosa, Veriscolor, Virginica", 
    pch = 24, bg = c("mediumorchid4", "mediumpurple1", "lightpink1")[unclass(iris$Species)], 
    upper.panel=NULL, labels=c("Sepal Length","Sepal Width","Petal Length","Petal Width"), 
    font.labels=1, cex.labels=2) 

legend(x = "topright",legend = levels(iris$Species), 
     pch = 24,pt.bg = c("mediumorchid4", "mediumpurple1", "lightpink1"), 
     inset = 0.1) 

而不是使用locator作爲喬希的建議,我只是直勾勾一個體面的設置爲inset

相關問題