r - 在 ggpairs (GGally) 中操作轴标题

标签 r scatter-plot ggally r-corrplot

我正在使用下面的代码来生成下面的图表。

# Setup
data(airquality)

# Device start
png(filename = "example.png", units = "cm", width = 20, height = 14, res = 300)

# Define chart
pairs.chrt <- ggpairs(airquality,
                      lower = list(continuous = "smooth"),
                      diag = list(continuous = "blank"),
                      upper = list(continuous = "blank")) +
  theme(legend.position = "none",
        panel.grid.major = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_text(angle = 180, vjust = 1, color = "black"),
        panel.border = element_rect(fill = NA))

# Device off and print
print(pairs.chrt)
dev.off()

ggpairs - First Example

我目前正在尝试修改 的显示轴标题 .特别是,我希望轴标题为:
  • 放置在距离轴标签更远的位置
  • 倾斜放置

  • 例如,我想获得轴 标题 类似于下图中的那些(我只对轴标签感兴趣,而不是在图表的其余部分):
    Example Label Placement
    取自:Geovisualist

    我尝试调整我的语法以更改 axis.title.x到不同的值,但它不会产生预期的结果。例如使用 angle = 45 运行代码.
    axis.title.x = element_text(angle = 45, vjust = 1, color = "black"),
                panel.border = element_rect(fill = NA))
    

    返回相同的图表。我可以通过更改 axis.text.x 来控制轴标签例如,但我找不到如何控制此图中的轴标题的答案。任何帮助都感激不尽。

    最佳答案

    简短的回答:似乎没有一种优雅或简单的方法可以做到这一点,但这里有一个解决方法。

    我挖了ggpairs源代码(在 GGally package source available from CRAN 中)查看变量标签的实际绘制方式。 ggpairs.R中的相关功能是 print.ggpairs .事实证明,变量标签不是 ggplot 的一部分。绘图矩阵的每个单元格中的对象——即它们不是轴标题,这就是为什么它们不受使用 theme(axis.title.x = element_text(angle = 45) 影响的原因或类似的。

    相反,它们似乎是使用 grid.text 绘制为文本注释。 (在包 'grid' 中)。 grid.text接受参数,包括 x, y, hjust, vjust, rot (其中 rot 是旋转角度),以及使用 gpar 的字体大小、字体系列等(见 ?grid.text ),但目前似乎没有办法将这些参数的不同值传递给 print.ggpairs -- 它们固定为默认值。

    您可以先将变量标签留空,然后使用修改 print.ggpairs 的相关部分,然后通过自定义放置、旋转和样式添加它们来解决这个问题。代码。我想出了以下修改。 (顺便说一句,因为原始 GGally 源代码是在 GPL-3 license 下发布的,所以这次修改也是如此。)

    customize.labels <- function(
      plotObj,
      varLabels = NULL, #vector of variable labels
      titleLabel = NULL, #string for title
      leftWidthProportion = 0.2, #if you changed these from default...
      bottomHeightProportion = 0.1, #when calling print(plotObj),...
      spacingProportion = 0.03, #then change them the same way here so labels will line up with plot matrix.
      left.opts = NULL, #see pattern in left.opts.default
      bottom.opts = NULL, #see pattern in bottom.opts.default
      title.opts = NULL) { #see pattern in title.opts.default
    
      require('grid')
    
      vplayout <- function(x, y) {
        viewport(layout.pos.row = x, layout.pos.col = y)
      }
    
      numCol <- length(plotObj$columns)
      if (is.null(varLabels)) {
        varLabels <- colnames(plotObj$data)
        #default to using the column names of the data
      } else if (length(varLabels) != numCol){
        stop('Length of varLabels must be equal to the number of columns')
      }
    
      #set defaults for left margin label style
      left.opts.default <- list(x=0,
                                y=0.5,
                                rot=90,
                                just=c('centre', 'centre'), #first gives horizontal justification, second gives vertical
                                gp=list(fontsize=get.gpar('fontsize')))
      #set defaults for bottom margin label style
      bottom.opts.default <- list(x=0,
                                  y=0.5,
                                  rot=0,
                                  just=c('centre', 'centre'),#first gives horizontal justification, second gives vertical
                                  gp=list(fontsize=get.gpar('fontsize')))
      #set defaults for title text style
      title.opts.default <- list(x = 0.5, 
                                 y = 1, 
                                 just = c(.5,1),
                                 gp=list(fontsize=15))
    
      #if opts not provided, go with defaults
      if (is.null(left.opts)) {
        left.opts <- left.opts.default
      } else{
        not.given <- names(left.opts.default)[!names(left.opts.default) %in% 
                                                names(left.opts)]
    if (length(not.given)>0){
      left.opts[not.given] <- left.opts.default[not.given]
    }
      }
    
    if (is.null(bottom.opts)) {
      bottom.opts <- bottom.opts.default
    } else{
      not.given <- names(bottom.opts.default)[!names(bottom.opts.default) %in%
                                                names(bottom.opts)]
    if (length(not.given)>0){
      bottom.opts[not.given] <- bottom.opts.default[not.given]
    }
    }
    
    if (is.null(title.opts)) {
      title.opts <- title.opts.default
    } else{
      not.given <- names(title.opts.default)[!names(title.opts.default) %in%
                                               names(title.opts)]
    if (length(not.given)>0){
      title.opts[not.given] <- title.opts.default[not.given]
    }
    }
    
      showLabels <- TRUE
      viewPortWidths <- c(leftWidthProportion, 
                          1, 
                          rep(c(spacingProportion,1), 
                              numCol - 1))
      viewPortHeights <- c(rep(c(1,
                                 spacingProportion), 
                               numCol - 1), 
                           1, 
                           bottomHeightProportion)
    
    viewPortCount <- length(viewPortWidths)
    
    if(!is.null(titleLabel)){
      pushViewport(viewport(height = unit(1,"npc") - unit(.4,"lines")))
      do.call('grid.text', c(title.opts[names(title.opts)!='gp'], 
                             list(label=titleLabel, 
                                  gp=do.call('gpar', 
                                          title.opts[['gp']]))))
      popViewport()
    }
    
      # viewport for Left Names
      pushViewport(viewport(width=unit(1, "npc") - unit(2,"lines"), 
                            height=unit(1, "npc") - unit(3, "lines")))
    
      ## new for axis spacingProportion
      pushViewport(viewport(layout = grid.layout(
        viewPortCount, viewPortCount,
        widths = viewPortWidths, heights = viewPortHeights
      )))
    
      # Left Side
      for(i in 1:numCol){
        do.call('grid.text', 
                c(left.opts[names(left.opts)!='gp'], 
                  list(label=varLabels[i], 
                       vp = vplayout(as.numeric(i) * 2 - 1 ,1),
                       gp=do.call('gpar', 
                               left.opts[['gp']]))))
      }
      popViewport()# layout
      popViewport()# spacing
    
      # viewport for Bottom Names
      pushViewport(viewport(width=unit(1, "npc") - unit(3,"lines"), 
                            height=unit(1, "npc") - unit(2, "lines")))
    
      ## new for axis spacing
      pushViewport(viewport(layout = grid.layout(
        viewPortCount, viewPortCount,
        widths = viewPortWidths, heights = viewPortHeights)))
    
      # Bottom Side
      for(i in 1:numCol){
        do.call('grid.text', 
                c(bottom.opts[names(bottom.opts)!='gp'], 
                  list(label=varLabels[i], 
                       vp = vplayout(2*numCol, 2*i),
                       gp=do.call('gpar', 
                               bottom.opts[['gp']]))))
      }
    
      popViewport() #layout
      popViewport() #spacing
    }
    

    这是调用该函数的示例:
    require('data.table')
    require('GGally')
    require('grid')
    fake.data <- data.table(test.1=rnorm(50), #make some fake data for  demonstration
                            test.2=rnorm(50), 
                            test.3=rnorm(50),
                            test.4=rnorm(50))
    
    g <- ggpairs(data=fake.data, 
                 columnLabels=rep('', ncol(fake.data)))
    #Set columnLabels to a vector of blank column labels
    #so that original variable labels will be blank.
    print(g)
    
    
    customize.labels(plotObj=g,
                     titleLabel = 'Test plot', #string for title
                     left.opts = list(x=-0.5, #moves farther to the left, away from vertical axis
                                      y=0.5, #centered with respect to vertical axis
                                      just=c('center', 'center'),
                                      rot=90,
                                      gp=list(col='red',
                                              fontface='italic',
                                              fontsize=12)), 
                     bottom.opts = list(x=0.5,
                                        y=0,
                                        rot=45, #angle the text at 45 degrees
                                        just=c('center', 'top'),
                                        gp=list(col='red',
                                                fontface='bold',
                                                fontsize=10)), 
                     title.opts = list(gp=list(col='green',
                                               fontface='bold.italic'))
    )
    

    (这会产生一些非常难看的标签——仅用于演示目的!)

    我没有修改将标签放置在左侧和底部以外的位置 - 就像在您的 Geovisualist 示例中一样 - 但我认为您可以通过将参数更改为 vplayout 来做到这一点。在 customize.labels 中的“左侧”和“底部”代码段中. xy grid.text 中的坐标是相对于视口(viewport)定义的,视口(viewport)将显示区域划分为网格
    pushViewport(viewport(layout = grid.layout(
            viewPortCount, viewPortCount,
            widths = viewPortWidths, heights = viewPortHeights
          )))
    

    调用 vplayout指定网格的哪个单元格用于定位每个标签。

    关于r - 在 ggpairs (GGally) 中操作轴标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28427572/

    相关文章:

    r - 编码 R 中向量中元素的变化

    r - 如果 dplyr 中满足条件,则直接对行进行条件求和

    google-visualization - Google Charts - 散点图 + 两个系列 + ChartRangeFilter - 问题

    r - 如何在 ggplot2 生成 Kaplan-Meier 图的置信区间中添加阴影和颜色?

    r - GGally::ggpairs 在绘制相关系数时没有网格线

    r - 在 R 中合并和保留变量标签

    r - 使用 merge() 使用第二个数据帧中的值更新数据帧

    python-3.x - Plotly 散点图趋势线出现在散点下方。如何让趋势线出现在散点图上? [Python]

    matlab - 如何在 Matlab 中旋转 2D 散点图

    r - 如何在R中通过GGally保存对象