r - pairs() 如何给不同的标签赋予不同的字体样式?

标签 r plot graph visualization correlation

在pairs()创建的相关图中,我想要第一个、第二个和第五个标签采用常规字体(font.label=1);第三个草书(font.label=3);第四和第六个标签以粗体显示(font.label=4)。我设法为不同的直方图赋予不同的颜色,但我未能为标签字体做同样的事情。

更改 font.label 会更改所有标签的字体样式,并且不接受多个数字。当尝试创建我自己的 text.panel 函数时,我不知道如何使字体样式动态化。

可重现的代码:

data("USJudgeRatings")
correlation <- USJudgeRatings[1:6]

clrs <- RColorBrewer::brewer.pal(n = 6, name = "RdBu")

panel.hist <- function(x, ...) {
  usr <- par("usr")
  on.exit(par(usr))
  par(usr = c(usr[1:2], 0, 1.5))
  his <- hist(x, plot = FALSE)
  breaks <- his$breaks
  nB <- length(breaks)
  y <- his$counts
  y <- y/max(y)
  c <<- c +1 
  rect(breaks[-nB], 0, breaks[-1], y, col = clrs[c] , ...) #different color per histogram
}


c <- 0
pairs(correlation,
      na.action = na.omit,
      diag.panel = panel.hist,# Adding the histograms
      #text.panel =   #Can we do something here? 
      #font.labels = c(1,1,3,2,1,2),   #Only takes one number
      cex.labels = 2,
      oma=c(1.4,1.4,1.4,1.4)) 

最佳答案

按照您的建议,您可以为 text.panel 提供自定义函数:

c <- 0
pairs(correlation,
      na.action = na.omit,
      diag.panel = panel.hist,# Adding the histograms
      text.panel = function(x, y, labels, cex, font, ...) 
        text(x, y, labels, font=c(1, 1, 3, 2, 1, 2)[c]),
      cex.labels = 2,
      oma=c(1.4, 1.4, 1.4, 1.4)) 

它使用与 panel.hist() 函数相同的全局变量 c 。我不能说我对这样的解决方案太满意(命名变量 c 也不是一个特别好的主意),但它确实完成了工作。

关于r - pairs() 如何给不同的标签赋予不同的字体样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74257011/

相关文章:

R:foreach 不适用于导出图形,例如 png 或 ggsave

Matlab 箱线图 : Use a specific percentile as the upper whisker OR remove redundant outliers after manual upper whisker edit

R:使用 par 设置多个绘图高度

algorithm - 使用 DFS 计算有向图中的循环数

matlab - 如何在 MATLAB 中绘制基本线图?

r - ggplot 并排 geom_bar()

python - Pandas 的数据框(Python)是否更接近 R 的数据框或数据表?

r - 用双标题在 R 中写入 .xlsx

python - 如何使用 Matplotlib 制作具有光环效果的散点图?

javascript - D3,变量 x 轴基于时间刻度