首页 > 解决方案 > ggtern - 刻面时扭曲的 hex bin 大小和形状

问题描述

我有一个问题,geom_hex_tern可以完美地处理单个图,但是当我制作刻面时,十六进制 bin 的大小和形状会变形。

library(tidyverse)
library(ggtern)

# My data
dat <- structure(list(Fact2 = c(0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 
  0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 
  0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 
  0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 
  0.24, 0.28, 0.28, 0.28, 0.28, 0.28), x = c(0.05, 0.1, 0.1, 0.1, 
    0.15, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.25, 0.25, 0.25, 0.25, 
    0.3, 0.3, 0.35, 0.35, 0.4, 0.4, 0.4, 0.45, 0.45, 0.45, 0.45, 
    0.5, 0.5, 0.5, 0.5, 0.55, 0.55, 0.55, 0.6, 0.6, 0.6, 0.65, 0.7, 
    0.75, 0.05, 0.1, 0.2, 0.3, 0.45), y = c(0.6, 0.5, 0.6, 0.7, 0.55, 
      0.1, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.35, 0.4, 0.45, 0.5, 0.3, 
      0.4, 0.25, 0.4, 0.3, 0.35, 0.4, 0.2, 0.25, 0.35, 0.45, 0.05, 
      0.15, 0.2, 0.25, 0.1, 0.2, 0.3, 0.05, 0.1, 0.25, 0.1, 0.05, 0.05, 
      0.55, 0.5, 0.55, 0.2, 0.25), z = c(0.35, 0.4, 0.3, 0.2, 0.3, 
        0.7, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2, 0.4, 0.35, 0.3, 0.25, 0.4, 
        0.3, 0.4, 0.25, 0.3, 0.25, 0.2, 0.35, 0.3, 0.2, 0.1, 0.45, 0.35, 
        0.3, 0.25, 0.35, 0.25, 0.15, 0.35, 0.3, 0.15, 0.25, 0.25, 0.2, 
        0.4, 0.4, 0.25, 0.5, 0.3), wt = c(0.027, 0.02, 0.016, 0.017, 
          0.043, 0.018, 0.02, 0.023, 0.037, 0.02, 0.018, 0.02, 0.015, 0.043, 
          0.031, 0.033, 0.036, 0.029, 0.015, 0.022, 0.036, 0.022, 0.017, 
          0.02, 0.022, 0.018, 0.019, 0.023, 0.02, 0.065, 0.038, 0.043, 
          0.02, 0.023, 0.063, 0.02, 0.018, 0.025, 0.042, 0.016, 0.015, 
          0.019, 0.017, 0.018, 0.039)), row.names = c(NA, -45L), class = c("tbl_df", 
            "tbl", "data.frame"))


# PLot Fact2 == 0.24 - OK
filter(dat, Fact2 == 0.24) %>%
  ggtern(aes(x = x, y = y, z = z)) + 
  geom_hex_tern(binwidth = 0.05, colour = "black",  aes(value = wt)) 

在此处输入图像描述

# PLot Fact2 == 0.28 - OK
filter(dat, Fact2 == 0.28) %>%
ggtern(aes(x = x, y = y, z = z)) + 
  geom_hex_tern(binwidth = 0.05, colour = "black", aes(value = wt)) 

在此处输入图像描述

# plot both together - weird hex bin size/shape 
ggtern(dat, aes(x = x, y = y, z = z)) + 
  geom_hex_tern(binwidth = 0.05, colour = "black", aes(value = wt)) +
  facet_wrap(~Fact2) 

在此处输入图像描述

前两个图看起来不错,但是当通过分面绘制在一起时,这些箱被弄乱了,这似乎只有在我绘制稀疏数据(几个箱)时才会发生,当我在每个图上有很多点时分面工作正常。任何关于如何让多面图看起来正常的建议将不胜感激。

标签: rggplot2ggtern

解决方案


我有一个可行的解决方案,尽管我不禁认为我已经完成了艰难的工作。

最初,由于您指出当要绘制大量 bin 时问题就会消失,所以我尝试尝试绘制大量额外的不可见六边形,并添加一个控制 alpha(透明度)的虚拟变量。不幸的是,当您使用分箱数据时,这不起作用。

我还尝试在不同的图层中创建不可见的六边形。这是可能的,但是在不同的层中具有不可见的六边形意味着它们不再将可见层中的六边形强制为正确的形状。

发生的另一个想法是尝试 2 x 2 刻面,因为我认为这会使六边形的形状正常化。它没有。

最后,我决定只“破解”ggplot,获取十六进制格罗布并在算术上更改它们的顶点。数学拉伸本身很简单,因为六角格子已经正确居中并且正好是所需高度的一半;因此,我们只取 y 坐标并从它们的值的两倍中减去它们范围的平均值。

棘手的部分是首先获得 grobs。首先,您需要将 ggplot 转换为 grobs 表(ggtern 有自己的功能来执行此操作)。这很简单,但 gTable 是一个深度嵌套的 S3 对象,因此要找到提取正确元素问题的通用解决方案是很棘手的。以正确的格式将它们放回原处很复杂,需要嵌套mapply函数。

然而,既然这已经完成,逻辑都可以包含在一个函数中,该函数只接受 ggplot 作为输入,然后用拉伸的 hex grobs 绘制版本(同时还默默地返回一个 gTable,以防你想用它做任何其他事情)

fix_hexes <- function(plot_object)
{
  # Define all the helper functions used in the mapply and lapply calls
  cmapply     <-  function(...)    mapply(..., SIMPLIFY = FALSE)
  get_hexes   <-  function(x)      x$children[grep("hex", names(x$children))]
  write_kids  <-  function(x, y) { x[[1]]$children <- y; return(x)}
  write_y     <-  function(x, y) { x$y <- y; return(x)}
  write_all_y <-  function(x, y) { gList <- mapply(write_y, x, y, SIMPLIFY = F)
                                   class(gList) <- "gList"; return(gList) }
  write_hex   <-  function(x, y) { x$children[grep("hex", names(x$children))] <- y; x; }
  fix_each    <-  function(y) {    yval <- y$y
                                   att  <- attributes(yval)
                                   yval <- as.numeric(yval)
                                   yval <- 2 * yval - mean(range(yval))
                                   att  -> attributes(yval)
                                   return(yval)}

  # Extract and fix the grobs
  g_table     <- ggtern::ggplot_gtable(ggtern::ggplot_build(plot_object))
  panels      <- which(sapply(g_table$grobs, function(x) length(names(x)) == 5))
  hexgrobs    <- lapply(g_table$grobs[panels], get_hexes)
  all_hexes   <- lapply(hexgrobs, function(x) x[[1]]$children)
  fixed_yvals <- lapply(all_hexes, lapply, fix_each)

  # Reinsert the fixed grobs
  fixed_hexes            <- cmapply(write_all_y, all_hexes, fixed_yvals)
  fixed_grobs            <- cmapply(write_kids, hexgrobs, fixed_hexes)  
  g_table$grobs[panels]  <- cmapply(write_hex, g_table$grobs[panels], fixed_grobs)

  # Draw the plot on a fresh page and silently return the gTable
  grid::grid.newpage()
  grid::grid.draw(g_table)
  invisible(g_table)
}

那么让我们看看原来的情节:

gg <- ggtern(dat, aes(x = x, y = y, z = z)) + 
       geom_hex_tern(binwidth = 0.05, colour = "black", aes(value = wt)) +
       facet_wrap(~Fact2)

plot(gg)

在此处输入图像描述

我们现在可以通过简单地修复它:

fix_hexes(gg)

在此处输入图像描述


推荐阅读