【问题标题】:How can I change the text on the diagonals of the scatter plots produced by the "pairs" function?如何更改“pairs”函数生成的散点图对角线上的文本?
【发布时间】:2019-10-29 13:20:07
【问题描述】:

我想更改 R 中以下代码生成的对角线上的文本(我想更改显示 Sepal.Length、Sepal.Width、Petal.Length 和 Petal.Width 的文本)。

我尝试在“pairs”函数中为 diag.panel 参数插入一些内容,但没有成功。

#Extracts the iris species column from the iris dataset
iris_class = iris$Species

#Change the class from characters to numerical values for indexing
# #1 = Iris-setosa
# #2 = Iris-versicolor
# #3 = Iris-virginica
class_to_number = as.numeric(factor(iris_class))


#A function to show the linear regression line in each graph
upper_panel_regression_line = function(x,y, ...){
  points(x,y,...)
  linear_regression = lm(y~x)
  linear_regression_line = abline(linear_regression)
}

#A function to calculate and show the R-squared value of each panel
lower_panel_r_squared = function(x, y, ...){
  usr <- par("usr"); on.exit(par(usr))
  par(usr = c(0, 2, 0, 2))
  r = cor(x, y)
  r_squared = round(r^2, digits = 2)
  text_of_r_squared = paste0("R-squared = ", r_squared)
  text(1, 1, text_of_r_squared)
}


pairs(iris[1:4], main = "Predicting Iris Class", 
lower.panel=lower_panel_r_squared, upper.panel = 
        upper_panel_regression_line, col=c("red","blue","green") [class_to_number])

【问题讨论】:

    标签: r plot


    【解决方案1】:

    这与@Axeman 非常接近,但我认为分配新标签的更加程序化的方式会有所帮助。

    首先,与往常一样,您可以更改输入数据的列名,使用colnamessetNames 或只是names。我假设这不是您想要的解决方案。

    使用text.panel= 参数有点麻烦,因为它必须预先定义以“知道”标签应该是什么,要么通过与旧标签进行比较,要么通过知道位置。我建议不要试图变得超级花哨,而是建议一种稍微花哨的方式。

    正如您在@Axeman 的回答中看到的(以及在?pairs 的文档中),通常text.panel= 和其他参数采用一个函数,但该函数的任何参数都不是用户可控的,因此我们将定义一个该函数首先使用要使用的新标签,然后返回一个传递给pairs 的函数。

    my.text.panel <- function(labels) {
      function(x, y, lbl, ...) {
        if (lbl %in% names(labels)) lbl <- labels[[lbl]]
        text(x, y, lbl, ...)
      }
    }
    
    pairs(iris[1:4], main = "Predicting Iris Class", 
          text.panel = my.text.panel(c(Sepal.Length="Slen", Sepal.Width="Swid",
                                       Petal.Length="Plen", Petal.Width="Pwid")),
          lower.panel = lower_panel_r_squared,
          upper.panel = upper_panel_regression_line,
          col=c("red","blue","green") [class_to_number])
    

    使用此功能,您可以指定全部、部分或不指定任何标签;如果没有找到新的映​​射,这里的默认设置是使用以前的标签,但可以很容易地在函数中扩展它来控制其他方法。

    结果:

    我最初保留了@Axeman 的答案中的mean(x) 等,但由于其他默认行为(使用名为textPanel 的内部定义函数)似乎期望单个数字而不是向量,我相信mean 是不必要的(虽然没有问题)。

    【讨论】:

    • 啊,是的,这更好:)
    【解决方案2】:
    diag_custom_labels <- function(x, y, labels, cex, font, ...) {
      if (!exists('i')) i <<- 1
      text(mean(x), mean(y), c('my', 'custom', 'diag', 'labels')[[i]], cex = cex)
      i <<- i + 1
    }
    
    pairs(iris[1:4], main = "Predicting Iris Class", 
          lower.panel=lower_panel_r_squared, upper.panel = upper_panel_regression_line, 
          text.panel = diag_custom_labels,
          col=c("red","blue","green") [class_to_number])
    

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 1970-01-01
      • 2021-10-29
      • 2016-08-23
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 2020-04-03
      • 2020-10-19
      相关资源
      最近更新 更多