【问题标题】:grid.arrange creates overlapping plotsgrid.arrange 创建重叠图
【发布时间】:2020-04-25 02:26:15
【问题描述】:

我目前正在构建一些包来绘制数据。一个目标是绘制三个彼此相邻的转速表。但是,当我使用 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 的错误还是我错过了什么?

【问题讨论】:

  • 请阅读 (1) 我如何提出一个好的问题,(2) 如何创建 MCVE 以及 (3) 如何在 R 中提供一个最小的可重现示例。

标签: r gridextra


【解决方案1】:

也许,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)

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

【讨论】:

  • @thban 如果确实解决了您的问题,请将答案标记为已解决;-)
  • 完成,感谢您的回答,它对我有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 2020-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-18
  • 2017-12-09
相关资源
最近更新 更多