pairs()将标签移动到散点图的侧面

pairs()将标签移动到散点图的侧面,r,R,对于iris数据,我们使用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)]) 将function panel.pearson定义如下: panel.pears

对于iris数据,我们使用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)])
将function panel.pearson定义如下:


panel.pearson这就是你的想法吗

# 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)
我通过反复试验确定了标签的位置。可能有更好的方法,但至少这样可以(几乎)将标签放在正确的位置

更新:A给了我一个稍微好一点的方法来定位轴标签的想法。正如链接的答案所指出的,您可以通过
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如何确定绘图区域边界和实际绘图开始位置之间的偏移量,那么也可以通过编程确定偏移量。

请提供可复制的代码?我的R不知道
面板。pearson
。谢谢Stephan。我错过了这个功能,并添加了它。最好的!谢谢你。然而,我正在考虑一种更加自动化的方式。也许,有一个带有离散标签的x轴和一个带有相同标签的右侧y轴。我也希望能够重复这样做。如果你能弄清楚
pairs
函数如何设置绘图区域的坐标,你可以将我上面的代码改编成一个函数,该函数将在每列下和每行绘图旁边添加居中的标签。是的,这正是我需要的。谢谢你!但是有什么建议吗?我该如何找到pairs函数如何为绘图区域设置坐标?@Nerdstat我已经更新了我的答案,并(希望)对你的问题给出了改进的答案。
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)