我不知道有什么内置函数,但编写一个函数来计算最空的象限并不困难.周的 comments .sf提到了"topright"等.这些关键字适用于legend
,但似乎在text
中不起作用,所以我将使用legend
来写文本.首先,功能
emptyQuadrant = function(x) {
mid1 = sum(range(x[,1]))/2
mid2 = sum(range(x[,2]))/2
Q1 = sum(x[,1]>=mid1 & x[,2]>=mid2)
Q2 = sum(x[,1]<=mid1 & x[,2]>=mid2)
Q3 = sum(x[,1]<=mid1 & x[,2]<=mid2)
Q4 = sum(x[,1]>=mid1 & x[,2]<=mid2)
BestQ = which.min(c(Q1, Q2, Q3, Q4))
return(c("topright", "topleft",
"bottomleft", "bottomright")[BestQ])
}
现在,您可以计算图例的最佳位置.
par(mfrow = c(1, 2))
plot(iris[ , 1:2])
legend(emptyQuadrant(iris[,1:2]), "some text here.", cex = 0.7, bty='n')
plot(iris[ , 3:4])
legend(emptyQuadrant(iris[,3:4]), "some text here.", cex = 0.7, bty='n')