grid.arrange 创建重叠图

grid.arrange creates overlapping plots

我目前正在构建一些包来绘制数据。一个目标是绘制三个彼此相邻的转速表图表。但是,当我使用 grid.arrange 执行此操作时,绘图以一种奇怪的方式重叠。 我的仪表函数如下:

library(grid)
library(ggplot2)

gg.gauge <- function(pos,breaks = c(0, 2.5, 5, 25)) {
  get.poly <- function(a, b, r1 = 0.5, r2 = 1.0) {
    th.start <- pi * (1 - a / 25)
    th.end   <- pi * (1 - b / 25)
    th       <- seq(th.start, th.end, length = 25)
    x        <- c(r1 * cos(th), rev(r2 * cos(th)))
    y        <- c(r1 * sin(th), rev(r2 * sin(th)))
    return(data.frame(x, y))
  }
  ggplot() +
    geom_polygon(data = get.poly(breaks[1], breaks[2]), aes(x, y), fill = "springgreen4") +
    geom_polygon(data = get.poly(breaks[2], breaks[3]), aes(x, y), fill = "khaki1") +
    geom_polygon(data = get.poly(breaks[3], breaks[4]), aes(x, y), fill = "indianred1") +
    geom_polygon(data = get.poly(pos - 0.1, pos + 0.1, 0.1), aes(x, y), fill = "navyblue") +
    geom_text(data = as.data.frame(breaks), size = 2, fontface = "bold", vjust = 0,
              aes(x = 1.1 * cos(pi * (1 - breaks / 25)), 
                  y = 1.1 * sin(pi * (1 - breaks / 25)), 
                  label = "")) +
    annotate("text", x = 0, y = 0, label = paste(pos, ""), vjust = 0, size = 4, fontface = "bold") +
    coord_fixed() +
    theme_bw() +
    theme(axis.text = element_blank(),
          axis.title = element_blank(),
          axis.ticks = element_blank(),
          panel.grid = element_blank(),
          panel.border = element_blank())
}

这是我简化的绘图函数:

myplot <- function () {
  mp <- grid.arrange(
    gg.gauge(2, breaks = c(0, 2.5, 5, 25)))
  return(mp)
}

然后这样调用:

lay <- rbind(c(1,2,3))
gridExtra::grid.arrange(myplot(), 
                        myplot(),
                        myplot(),
                        layout_matrix = lay)

使用 ncol, nrow 而不是 layout_matrix :

gridExtra::grid.arrange(myplot(), 
                        myplot(),
                        myplot(),
                        ncol = 3,
                        nrow = 1)

结果完全一样。这是 grid.arrange 的错误还是我遗漏了什么?

也许,双重使用gid.arrange()不行?查看我更改的 myplot() 函数:

library(grid)
library(ggplot2)
library(gridExtra)

get.poly <- function(a, b, r1 = 0.5, r2 = 1.0) {
  th.start <- pi * (1 - a / 25)
  th.end   <- pi * (1 - b / 25)
  th       <- seq(th.start, th.end, length = 25)
  x        <- c(r1 * cos(th), rev(r2 * cos(th)))
  y        <- c(r1 * sin(th), rev(r2 * sin(th)))
  return(data.frame(x, y))
}

gg.gauge <- function(pos,breaks = c(0, 2.5, 5, 25)) {
  ggplot() +
    geom_polygon(data = get.poly(breaks[1], breaks[2]), aes(x, y), fill = "springgreen4") +
    geom_polygon(data = get.poly(breaks[2], breaks[3]), aes(x, y), fill = "khaki1") +
    geom_polygon(data = get.poly(breaks[3], breaks[4]), aes(x, y), fill = "indianred1") +
    geom_polygon(data = get.poly(pos - 0.1, pos + 0.1, 0.1), aes(x, y), fill = "navyblue") +
    geom_text(data = as.data.frame(breaks), size = 2, fontface = "bold", vjust = 0,
              aes(x = 1.1 * cos(pi * (1 - breaks / 25)), 
                  y = 1.1 * sin(pi * (1 - breaks / 25)), 
                  label = "")) +
    annotate("text", x = 0, y = 0, label = paste(pos, ""), vjust = 0, size = 4, 
             fontface = "bold") +
    coord_fixed() +
    theme_bw() +
    theme(axis.text = element_blank(),
          axis.title = element_blank(),
          axis.ticks = element_blank(),
          panel.grid = element_blank(),
          panel.border = element_blank())
}

myplot <- function () {
  # mp <- grid.arrange(
  #   gg.gauge(2, breaks = c(0, 2.5, 5, 25)))
  mp <- gg.gauge(2, breaks = c(0, 2.5, 5, 25))
  return(mp)
}


lay <- rbind(c(1,2,3))
gridExtra::grid.arrange(myplot(), 
                        myplot(),
                        myplot(),
                        layout_matrix = lay)

gridExtra::grid.arrange(myplot(), 
                        myplot(),
                        myplot(),
                        ncol = 3,
                        nrow = 1)

然后两个版本创建相同的情节: