如何在R中向图表添加自定义图例?

时间:2013-10-14 18:13:11

标签: r

我一直在pairs()工作,并且花了很长时间试图添加一个图例或使用幻影符号来改变标题中物种的颜色以与点相关...任何洞察力将不胜感激!这是一个问题。

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

1 个答案:

答案 0 :(得分: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设置了一个不错的设置。