2

I'm currently building some packages to plot data. One goal is to plot three tachometer charts next to each other. However when i do this with grid.arrange, the plots are overlapping in a strange way. My gauge function is following:

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())
}

Here is my simplified plot function:

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

which is then called like this:

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

Working with ncol, nrow instead of layout_matrix :

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

has exactly the same results. Is this a bug by grid.arrange or am i missing something?

thban
  • 43
  • 5
  • Please read [(1)](https://stackoverflow.com/help/how-to-ask) how do I ask a good question, [(2)](https://stackoverflow.com/help/mcve) how to create a MCVE as well as [(3)](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) how to provide a minimal reproducible example in R. – Christoph Apr 29 '20 at 12:12

1 Answers1

1

Perhaps, the double use of grid.arrange() does not work? See my changed myplot() function:

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)

Then both versions create the same plot:

enter image description here

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Christoph
  • 6,841
  • 4
  • 37
  • 89