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?
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: