如何对齐垂直堆叠的图(由ggplot制作)

时间:2019-02-13 11:49:59

标签: r ggplot2 grid grob

我有两个要垂直对齐的线图(使用ggplot创建)。我基本上已经做到了。但是由于第二个图的y轴上的值较大,所以图的内部宽度不相同,因此它们无法对齐(请参见红色标记):enter image description here

两个图的x轴完全相同,这就是为什么我真的希望它们完全对齐以使其看起来不错。

我尝试设置“宽度”值,但这似乎只会影响水平对齐的图,而不会垂直对齐。

我的数据:

structure(list(`J01 ATC-Gruppe` = structure(c(1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("Tetrazykline", 
"Penicilline", "Andere Beta-Lactame", "Sulfonamide & Trimethoprim", 
"Makrolide, Lincosamide & Streptogramine", "Aminoglykoside", 
"Chinolone", "Andere Antibiotika"), class = "factor"), Monat = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L), .Label = c("Jan 2018", "Feb 2018", "Mär 2018", 
"Apr 2018", "Mai 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", 
"Okt 2018", "Nov 2018", "Dez 2018"), class = "factor"), DDDs = c(262, 
5729, 3305, 495, 588, 153, 944, 922, 85, 6386, 3157, 546, 753, 
120, 421, 419, 102, 6758, 3582, 682, 853, 167, 884, 1061, 122, 
5628, 3395, 564, 520, 161, 501, 728, 208, 4976, 3380, 508, 620, 
205, 704, 1106, 147, 4420, 3521, 634, 611, 110, 728, 1212, 133, 
5717, 3783, 708, 582, 230, 593, 842, 387, 6191, 3796, 605, 410, 
62, 595, 875, 130, 5309, 4084, 2010, 395, 163, 632, 1202, 84, 
5266, 3437, 617, 568, 141, 623, 948, 60, 5972, 3485, 718, 518, 
132, 588, 1034, 182, 4924, 3360, 573, 405, 96, 319, 1337)), row.names = c(NA, 
-96L), class = "data.frame")

structure(list(`J01 ATC-Gruppe` = structure(c(1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("Tetrazykline", 
"Penicilline", "Andere Beta-Lactame", "Sulfonamide & Trimethoprim", 
"Makrolide, Lincosamide & Streptogramine", "Aminoglykoside", 
"Chinolone", "Andere Antibiotika"), class = "factor"), Monat = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L), .Label = c("Jan 2018", "Feb 2018", "Mär 2018", 
"Apr 2018", "Mai 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018", 
"Okt 2018", "Nov 2018", "Dez 2018"), class = "factor"), SUs = c(262, 
10346, 5315, 1621, 1086, 321, 2639, 1047, 85, 11740, 4978, 1505, 
1475, 271, 1031, 928, 102, 12326, 6055, 1974, 1545, 382, 2113, 
1373, 170, 10302, 5777, 1630, 1048, 348, 1529, 958, 208, 7516, 
5578, 1631, 1069, 441, 2540, 1212, 185, 6536, 5852, 1945, 1474, 
259, 1619, 1272, 169, 9977, 6026, 2013, 1108, 523, 1636, 1185, 
392, 10442, 6082, 1854, 837, 137, 1153, 1102, 130, 9817, 6531, 
5265, 880, 373, 1296, 1707, 120, 9747, 5445, 1748, 1127, 349, 
1541, 1561, 85, 10792, 5648, 2265, 899, 232, 1659, 1177, 252, 
9177, 5307, 1660, 796, 231, 698, 1302)), row.names = c(NA, -96L
), class = "data.frame")

我的代码:

USB_J01_DDD_nototal_long_Lineplot <- ggplot(USB_J01_DDD_nototal_long, aes(fill = `J01 ATC-Gruppe`, y = `DDDs`, x = Monat, colour = `J01 ATC-Gruppe`, group = `J01 ATC-Gruppe`)) + geom_line(size = 1) + geom_point(size = 2) +
  theme(legend.position="bottom", 
    legend.title=element_blank(), 
    axis.text.x = element_text(angle = 45, hjust = 1)) +
  guides(fill=guide_legend(nrow=3, byrow=TRUE)) +
  theme(axis.title.x=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank())

USB_J01_SU_nototal_long_Lineplot <- ggplot(USB_J01_SU_nototal_long, aes(fill = `J01 ATC-Gruppe`, y = `SUs`, x = Monat, colour = `J01 ATC-Gruppe`, group = `J01 ATC-Gruppe`)) +
  geom_line(size = 1) + geom_point(size = 2) +
  theme(legend.position="bottom", legend.title=element_blank(), axis.text.x = element_text(angle = 45, hjust = 1)) +
  guides(fill=guide_legend(nrow=3, byrow=TRUE))

USB_J01_absolut_DDDs_vs_SUs_legend <- g_legend(USB_J01_DDD_nototal_long_Lineplot) #Legende von USB_J01_DDD_nototal_long_Lineplot extrahieren

grid.arrange(arrangeGrob(USB_J01_DDD_nototal_long_Lineplot + theme(legend.position="none"),
                     USB_J01_SU_nototal_long_Lineplot + theme(legend.position="none"),
                     nrow=2, heights = c(0.455,0.545)),
         USB_J01_absolut_DDDs_vs_SUs_legend, nrow=2, heights=c(0.9,0.1))

我希望两个图的内部宽度完全相同,以便x轴对齐,我该如何设法做到这一点?

0 个答案:

没有答案