2015-01-22 15 views
6

facet_grid cho phép tôi kích thước mỗi chiều rộng khía cạnh theo số hạng mục trên trục y (space lập luận):'Nhãn trên đỉnh' với facet_grid, hoặc 'tùy chọn không gian' với facet_wrap

df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6)) 
ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
ylab("") + 
theme(strip.text.y = element_text(angle=0)) 

enter image description here

Nhưng tôi muốn nhãn khía cạnh trên đầu trang, vì vậy tôi chuyển sang facet_wrap, và mất space luận (khía cạnh có tất cả chiều rộng tương tự):

ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_wrap(~ label, scales = "free_y", ncol = 1) + 
ylab("") 

enter image description here

Có thể tận dụng tối đa cả hai thế giới?

Cảm ơn bạn trước sự giúp đỡ của bạn.

Trả lời

3

Nó có thể được thực hiện bằng tay. Tỷ lệ chiều cao của ba tấm trong ô mong muốn là khoảng 1: 3: 2. Các mực nước trong ba tấm có thể được điều chỉnh bằng cách thay đổi grobs:

library(ggplot2) 
library(grid) 
df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6)) 

p1 = ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_wrap(~ label, scales = "free_y", ncol = 1) + 
ylab("") 

g1 = ggplotGrob(p1) 

g1$heights[[7]] = unit(1, "null") 
g1$heights[[12]] = unit(3, "null") 
g1$heights[[17]] = unit(2, "null") 

grid.newpage() 
grid.draw(g1) 

Hoặc, chiều cao có thể được thiết lập để được giống như những người trong cốt truyện gốc:

p2 = ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
ylab("") + 
theme(strip.text.y = element_text(angle=0)) 

g2 = ggplotGrob(p2) 

g1$heights[[7]] = g2$heights[[6]] 
g1$heights[[12]] = g2$heights[[8]] 
g1$heights[[17]] = g2$heights[[10]] 

grid.newpage() 
grid.draw(g1) 

Hoặc, những đỉnh cao có thể được đặt mà không cần tham chiếu đến ô gốc. Chúng có thể được đặt theo số lượng items cho mỗi label trong df. Và vay một số mã từ @baptiste's answer here để chọn các mục từ cách bố trí tương ứng với tấm:

# From 'df', get the number of 'items' for each 'label'. 
# That is, the number y-breaks in each panel. 
library(plyr) 
N = dlply(df, .(label), function(x) length(row.names(x))) 

# Get the items in the g1 layout corresponding to the panels. 
panels1 <- g1$layout$t[grepl("panel", g1$layout$name)] 

# Replace the default panel heights with relative heights 
g1$heights[panels1] <- unit(N, "null") 

## Draw g1 
grid.newpage() 
grid.draw(g1) 

enter image description here

4

Tôi không biết làm thế nào để làm điều đó trong ggplot, nhưng bạn có thể đạt được một cách bố trí tương tự với một phong cách oid ggplot sử dụng latticeExtra:

library(latticeExtra) 
df$label <- factor(df$label, levels = unique(df$label)) 

dotplot(item ~ value | label, data = df, 
     layout = c(1, 3), 
     as.table = TRUE, 
     scales = list(y = list(relation = "free")), 
     par.settings = ggplot2like()) 
resizePanels() 

enter image description here

Các vấn đề liên quan