【问题标题】:pairs() move labels to sides of scatter plotpair() 将标签移动到散点图的两侧
【发布时间】:2012-11-13 19:19:15
【问题描述】:

对于虹膜数据,我们使用pairs()函数得到散点图,如下所示:

pairs(iris[1:4], 
      main = "Edgar Anderson's Iris Data", 
      lower.panel=panel.pearson, 
      pch = 21, 
      bg = c("red", "green3", "blue")[unclass(iris$Species)])

函数panel.pearson定义如下:

panel.pearson <- function(x, y, ...) { horizontal <- (par("usr")[1] + par("usr")[2]) / 2; vertical <- (par("usr")[3] + par("usr")[4]) / 2; text(horizontal, vertical, format(abs(cor(x,y)), digits=2)) }

我需要将下面板转换为相关矩阵并从对角线上移除标签并将它们放在右轴和下轴上。我尝试了以下方法:

pairs(iris[1:4], 
      main = "Edgar Anderson's Iris Data", 
      labels=NULL, 
      lower.panel=panel.pearson, 
      xaxt='n', 
      yaxt='n', 
      pch = 21, 
      bg = c("red", "green3", "blue")[unclass(iris$Species)])

这给了我我需要的东西。除了我不明白如何获取底部和右侧轴上的标签(变量标签,我的意思是 Sepal.Length、Sepal.Width 等)。非常感谢任何帮助。谢谢!

【问题讨论】:

  • 能否提供可重现的代码?我的 R 不知道panel.pearson
  • 谢谢斯蒂芬。我错过了该功能并添加了它。最好的!

标签: r


【解决方案1】:

这是你的想法吗?

# Horizontal axis
text(seq(.2, 2, length.out=4), 0,
     c("Sepal Length","Sepal Width","Petal Length","Petal Width"),
     xpd=TRUE, adj=c(0,.5), cex=.9)

# Vertical axis
text(0, seq(0.35, 2.05, length.out=4),
     rev(c("Sepal Length","Sepal Width","Petal Length","Petal Width")),
     xpd=TRUE, adj=c(0.5, 0), 
     srt=90,  # rotates text to be parallel to axis
     cex=.9)

我通过反复试验来定位标签。可能有更好的方法,但至少这会将标签放在(几乎)正确的位置。

更新new SO question 给了我一个想法,让我找到了一种更好的方式来定位轴标签。正如链接的答案所指出的,您可以使用par('usr') 获取绘图区域的当前坐标。所以这里是对代码的更新,基于此:

x.coords = par('usr')[1:2]
y.coords = par('usr')[3:4]

# Offset is estimated distance between edge of plot area and beginning of actual plot
x.offset = 0.03 * (x.coords[2] - x.coords[1])  
xrng =  (x.coords[2] - x.coords[1]) - 2*x.offset
x.width = xrng/4  

y.offset = 0.028 * (y.coords[2] - y.coords[1])
yrng =  (y.coords[2] - y.coords[1]) - 2*y.offset
y.width = yrng/4  

# seq function below calculates the location of the midpoint of each panel

# x-axis labels
text(seq(x.coords[1] + x.offset + 0.5*x.width, x.coords[2] - x.offset - 0.5*x.width,
         length.out=4), 0,
     c("Sepal Length","Sepal Width","Petal Length","Petal Width"),
     xpd=TRUE,adj=c(.5,.5), cex=.9)

# y-axis labels
text(0, seq(y.coords[1] + y.offset + 0.5*y.width, y.coords[2] - 3*y.offset - 0.5*y.width, 
     length.out=4),
     rev(c("Sepal Length","Sepal Width","Petal Length","Petal Width")),
     xpd=TRUE, adj=c(0.5, 0.5), 
     srt=90,  # rotates text to be parallel to axis
     cex=.9)

仍然不理想,因为偏移量的大小是通过反复试验确定的。如果有人知道 R 如何确定绘图区域边界与实际绘图开始位置之间的偏移量大小,那么也可以通过编程方式确定偏移量。

【讨论】:

  • 感谢 eipi10。但是,我正在考虑一种更自动化的方式。也许,有一个带有离散标签的 x 轴和一个带有相同标签的右侧的 y 轴。我也希望能够反复这样做。
  • 如果您能弄清楚pairs 函数如何布置绘图区域的坐标,您可以将我上面的代码改编成一个函数,该函数将在每列下方和旁边添加居中的标签每行地块。
  • 是的,这正是我所需要的。谢谢eipi10!但是有什么建议我如何找出pairs函数如何布置绘图区域的坐标?
  • @Nerdstat 我已经用(希望)改进了对您问题的答案更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2015-03-22
  • 2023-04-09
  • 2010-12-21
  • 1970-01-01
相关资源
最近更新 更多