【问题标题】:How can I get legend for coplot in r?如何在 r 中获得 coplot 的图例?
【发布时间】:2017-01-18 18:08:07
【问题描述】:

使用iris 数据集,当我将点的颜色定义为variable 变量(在本例中为Species)时,我将找到一种方法来获取coplot 中的图例。 换句话说,我想看一个图例来告诉我哪种形状和颜色代表了哪些物种

下面是脚本

coplot(Sepal.Width~Sepal.Length|Petal.Width*Petal.Length, data = iris,
number=c(3,3),overlap=.5,col=as.numeric(iris$Species),
pch=as.numeric(iris$Species)+1)

这是生成的图表:

【问题讨论】:

  • 你可以用legend .. legend("topright", legend=unique(iris$Species), col=1:3, pch=2:4, xpd=NA) 来做这件事,虽然可能想找到一个更好的位置

标签: r plot data-visualization lattice


【解决方案1】:
coplot(Sepal.Width~Sepal.Length|Petal.Width*Petal.Length, data = iris,
       number=c(3,3),overlap=.5,col=as.numeric(iris$Species),
       pch=as.numeric(iris$Species)+1)

legend("topright", pch = unique(as.numeric(iris$Species)+1), 
       col = unique(as.numeric(iris$Species)), 
       legend = unique(iris$Species))

您只需将图例位置调整为更适合您的图形大小。

【讨论】:

  • 您可以使用 locator(1) 找到您想要放置绘图左上角的位置的坐标。
  • @G5W 绝对!
  • 要获得任何命令类型的帮助?command 所以?legend?locator 将是有序的。定位器,让您单击现有图表并告诉您单击位置的 x-y 坐标。 “topright”不适用于我的图表,所以我尝试使用从定位器派生的 x-y 坐标放置图例。
  • 略显晦涩,但也在文档?legend 中。您可以使用 bty='n'。
  • 使用locator(1) 并单击图例所需位置的图。 R 将返回 x 和 y 位置值。然后将位置添加到图例中,例如legend(x = 3, y = 7.5, ...,代替“topright”。这个位置是指图例的左上角。
猜你喜欢
  • 1970-01-01
  • 2020-11-23
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多