2017-06-07 18 views
2

ggarrangeを使用して2つのファセットプロットを配置したいと思います(x軸を整列させるため)。ggarrangeを使用すると軸線が消えるのを避けるには?

library(egg) 
library(ggplot2) 

p1 <- ggplot(warpbreaks) + 
    geom_bar(aes(x = wool)) + 
    facet_wrap(~tension, ncol = 2, scales = "free_x") + 
    theme_bw() + 
    theme(axis.line = element_line(colour = "black", size = .1), 
     panel.border = element_blank(), 
     strip.background = element_blank()) 

p2 <- ggplot(warpbreaks) + 
    geom_bar(aes(x = tension)) + 
    facet_wrap(~wool) + 
    theme_bw() + 
    theme(axis.line = element_line(colour = "black", size = .1), 
     panel.border = element_blank(), 
     strip.background = element_blank()) 

ggarrange(p1, p2, ncol = 2) 

enter image description here

素晴らしい作品が、残念ながら、縦軸ラインが消えました。これはgrid.arrangeを使用しても起こりませんが、少なくとも私の実際のデータにはx軸が整列していないので、ggarrangeを使用したいと考えています。軸線を維持する方法はありますか?

+2

はy軸線のクリッピングの問題に関連すると思われます。それは完全には消えませんが、その半分はプロットパネルによって切り取られます。 'panel.background = element_blank()'を追加してみてください。 – baptiste

答えて

4

tl; dr:軸を復元する必要があります。panel.background = element_blank()

ggplot2のクリッピングの問題(y軸の線は、プロットパネルで切り捨てられ、幅が半分になる)と、egg :: gtable_frameがプロットパネルの下に軸を配置していると思います。

library(egg) 
library(ggplot2) 

p1 <- ggplot(warpbreaks) + 
    geom_bar(aes(x = wool)) + 
    facet_wrap(~tension, ncol = 2, scales = "free_x") + 
    theme_bw() + 
    theme(axis.line = element_line(colour = alpha("red", 0.5), size = 5), 
     panel.border = element_blank(), 
     panel.background = element_rect(fill = alpha("white", 0.5), 
             linetype = 2, colour = "black"), 
     strip.background = element_blank()) 

p1 

enter image description here

g1 <- ggplotGrob(p1) 
gg <- gtable_frame(g1) 
grid.newpage() 
grid.draw(gg) 

enter image description here

関連する問題