ggplot:使用相同的绘图比例在一个pdf中保存多个绘图

时间:2017-09-14 11:24:12

标签: r ggplot2

我想在一个pdf中保存多个ggplots。这非常有效。问题是不同的图表确实有不同的y轴文本。结果是实际绘图(x和y轴)的不同长度,因为y轴描述具有不同的长度。我需要以某种方式硬编码x轴的确切宽度,以便绘图停止不对齐。

以下是问题的一个示例:Link

这里是可重现的代码:

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

2 个答案:

答案 0 :(得分:1)

解决方案1:

将新行(\n)添加到ylab(即ylab("module\n\n\n\n\n"))。像这样你会增加轴标题和情节之间的空间。

a <- ggplot() + 
  geom_point(data = perWorkField, mapping = aes(x = percent, y = module)) +
  xlim(c(0, 100)) +
  ylab("module\n\n\n\n\n")

解决方案2:

非常脏的黑客:为文本添加空白区域。我的解决方案的问题是您必须手动调整nWhiteSpace(即,它不等于nchar("ThisIsALongText"))。

nWhiteSpace <- 24
foo <- paste(c(rep(" ", nWhiteSpace), "A"), collapse = "")
foo
[1] "                        A"


df1 <- perWorkField %>% mutate(module = replace(module, module == "A", foo))

a <- ggplot() + 
  geom_point(data = df1, 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()

enter image description here

答案 1 :(得分:1)

此解决方案基于以下帖子:

Get width of plot area in ggplot2

我为两个图提取了x轴长度,然后将更宽的图偏移了 两个x轴长度的差异。

编辑:

事实证明这种解决方案并不完美。绘图略有偏移,因此该解决方案还需要手动调整。

library(dplyr)
library(ggplot2)
library(grid)

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

a
current.vpTree()
seekViewport('xlab-t.4-4-4-4')
x_axis_a <- convertWidth(unit(1,'npc'), 'inch', TRUE)
x_axis_a

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

b
current.vpTree()
seekViewport('xlab-t.4-4-4-4')
x_axis_b <- convertWidth(unit(1,'npc'), 'inch', TRUE)
x_axis_b

a <- a + theme(plot.margin = unit(c(1,1,1,1+abs(x_axis_b - x_axis_a)),"inch"))
b <- b + theme(plot.margin = unit(c(1,1,1,1),"inch"))

pdf("test.pdf")
print(list(a, b))
dev.off()

感觉非常hacky,但为我做了诀窍。