2015-09-15 10 views
12

Tworzę kilka fasetowanych wykresów dla raportu. Liczba płaszczyzn waha się między 2 a 8. Idealnie, chciałbym, aby bezwzględny rozmiar każdego aspektu (na całej powierzchni) był taki sam (np. 4x4 cm), aby łatwiej było je porównać (i wygląda ładniej).Ustawianie bezwzględnego rozmiaru faset w ggplot2

Czy to możliwe?

df1 <- structure(list(group1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, 1L), .Label = c("S1", "S2"), class = "factor"), group = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C", "D", "E"), class = "factor"), value = 1:12), class = "data.frame", row.names = c(NA, -12L), .Names = c("group1", "group", "value")) 

df2 <- structure(list(group1 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("S1", "S2"), class = "factor"), group = structure(c(4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), .Label = c("A", "B", "C", "D", "E"), class = "factor"), value = 13:20), class = "data.frame", row.names = c(NA, -8L), .Names = c("group1", "group", "value")) 

library(ggplot2) 

plot1 <- ggplot(df1) + geom_histogram(aes(x=value)) + facet_wrap(~group) 
plot2 <- ggplot(df2) + geom_histogram(aes(x=value)) + facet_wrap(~group) 

enter image description here enter image description here

Odpowiedz

10

Użyłem następującą funkcję do tego celu,

set_panel_size <- function(p=NULL, g=ggplotGrob(p), file=NULL, 
          margin = unit(1,"mm"), 
          width=unit(4, "cm"), 
          height=unit(4, "cm")){ 

    panels <- grep("panel", g$layout$name) 
    panel_index_w<- unique(g$layout$l[panels]) 
    panel_index_h<- unique(g$layout$t[panels]) 
    nw <- length(panel_index_w) 
    nh <- length(panel_index_h) 

if(getRversion() < "3.3.0"){ 

    # the following conversion is necessary 
    # because there is no `[<-`.unit method 
    # so promoting to unit.list allows standard list indexing 
    g$widths <- grid:::unit.list(g$widths) 
    g$heights <- grid:::unit.list(g$heights) 

    g$widths[panel_index_w] <- rep(list(width), nw) 
    g$heights[panel_index_h] <- rep(list(height), nh) 

} else { 

    g$widths[panel_index_w] <- rep(width, nw) 
    g$heights[panel_index_h] <- rep(height, nh) 

} 

    if(!is.null(file)) 
    ggsave(file, g, 
      width = convertWidth(sum(g$widths) + margin, 
           unitTo = "in", valueOnly = TRUE), 
      height = convertHeight(sum(g$heights) + margin, 
            unitTo = "in", valueOnly = TRUE)) 

    g 
} 

g1 <- set_panel_size(plot1) 
g2 <- set_panel_size(plot2) 
grid.arrange(g1,g2) 

enter image description here

+1

Dzięki, chociaż ja wciąż nie rozumiem, jak korzystać z tej funkcji bez odbieranie komunikatu o błędzie. Czy możesz rozwinąć swoją odpowiedź, pokazując, jak uzyskać pożądany wynik za pomocą moich przykładowych danych? – beetroot

+0

Po prostu próbowałem tego z R Devel, a ta linia nie działa dla mnie: 'g $ widths [panel_index_w] <- rep (lista (szerokość), nw)'. Muszę użyć tego: 'g $ widths [panel_index_w] <- rep (grid ::: unit.list (width), nw)'. –

+0

Aby odpowiedzieć na mój wcześniejszy komentarz: wiersz 'g $ widths [panel_index_w] <- rep (lista (szerokość), nw) należy zastąpić' g $ widths [panel_index_w] <- rep (width, nw) '. Wtedy wszystko działa. –