I want to save multiple ggplots in one pdf. That works pretty well. The problem is that the different plots do have different y axis texts. The result is a different length of the actual plot (x and y axes) due to the fact, that the y axis description has different lengths. I need to hard code somehow the exact width of the x axis so the plot stops misaligning.
Here's an example of the problem: Link
Here's the reproducable code:
library(dplyr)
library(ggplot2)
v1 <- c(1, 1, 1); v2 <- c(1, 0, 0)
v3 <- c(1, 0, 1); v4 <- c(0, 1, 1)
v5 <- c(1, 0, 1); xG <- c(3, 3, 3, 3, 3)
input <- c(v1, v2, v3, v4, v5)
df <- data_frame(values = input,
module = c(rep("A", length(v1)),
rep("B", length(v2)),
rep("C", length(v3)),
rep("D", length(v4)),
rep("E", length(v5))))
perWorkField <- df %>%
group_by(module) %>%
summarise(sums = sum(values)) %>%
mutate(percent = round((sums / xG) * 100, 2))
a <- ggplot() +
geom_point(data = perWorkField, mapping = aes(x = percent, y = module)) +
xlim(c(0, 100))
df2 <- perWorkField %>% mutate(module = replace(module, module == "A", "ThisIsALongText"))
b <- ggplot() +
geom_point(data = df2, mapping = aes(x = percent, y = module)) +
xlim(c(0, 100))
pdf("test.pdf")
print(list(a, b))
dev.off()