2012-08-09 27 views
27

Có cách nào để nhận lưới.arrange() để hoạt động như split.screen()? Tôi muốn sắp xếp một bảng để được đặt trực tiếp bên dưới huyền thoại.Chèn một bảng dưới chú giải trong biểu đồ ggplot2

#create histogram 
my_hist<-ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() 

#create inset table 
my_table<- tableGrob(head(diamonds)[,1:3],gpar.coretext =gpar(fontsize=8),gpar.coltext=gpar(fontsize=8), gpar.rowtext=gpar(fontsize=8)) 

grid.arrange(my_hist,my_table, ncol=2) 

sản xuất:

enter image description here

nhưng tôi muốn nó trông gần như thế này:

enter image description here

tôi đã cố gắng split.screen() nhưng nó dường như không để làm việc với đồ họa loại ggplot. Bất kỳ đề xuất? Cảm ơn.

+0

Kiểm tra này [link] (http://learnr.wordpress.com/2009/04/29/ggplot2-labelling-data-series-and-adding-a-data-table /) ngoài. Tôi cần phải làm điều tương tự một thời gian trước đây, mặc dù tôi không chắc liệu mã ở đây có lỗi thời hay không. –

+0

Đây là một câu hỏi cũ, bạn phải thay đổi 'opts' trong các câu trả lời dưới đây nếu bạn muốn làm cho chúng hoạt động. – durum

Trả lời

25

câu trả lời Dickoa là rất gọn gàng. Mỏ cho phép bạn kiểm soát nhiều hơn các yếu tố.

my_hist <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() 

#create inset table 
my_table <- tableGrob(head(diamonds)[,1:3], gpar.coretext = gpar(fontsize=8), gpar.coltext=gpar(fontsize=8), gpar.rowtext=gpar(fontsize=8)) 

#Extract Legend 
g_legend <- function(a.gplot){ 
    tmp <- ggplot_gtable(ggplot_build(a.gplot)) 
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
    legend <- tmp$grobs[[leg]] 
    return(legend)} 

legend <- g_legend(my_hist) 

#Create the viewports, push them, draw and go up 
grid.newpage() 
vp1 <- viewport(width = 0.75, height = 1, x = 0.375, y = .5) 
vpleg <- viewport(width = 0.25, height = 0.5, x = 0.85, y = 0.75) 
subvp <- viewport(width = 0.3, height = 0.3, x = 0.85, y = 0.25) 
print(my_hist + opts(legend.position = "none"), vp = vp1) 
upViewport(0) 
pushViewport(vpleg) 
grid.draw(legend) 
#Make the new viewport active and draw 
upViewport(0) 
pushViewport(subvp) 
grid.draw(my_table) 

enter image description here

+0

rất rõ ràng ... và kiểm soát tốt hơn đầu ra – dickoa

+0

@Iselzer Cảm ơn lời khuyên của bạn! Nhiều đánh giá cao – Elizabeth

12

Trước tiên, bạn nên xem qua số Wiki này, có nhiều ví dụ (xem phần sắp xếp). Vì vậy, sử dụng ví dụ thoses, tôi quản lý để có giải pháp này

require(gridExtra) 
require(ggplot2) 

## original graph 
my_hist <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() 

## get the legend 
tmp <- ggplot_gtable(ggplot_build(my_hist)) 
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
legend <- tmp$grobs[[leg]] 

## create inset table 
my_table <- tableGrob(head(diamonds)[,1:3],gpar.coretext =gpar(fontsize=8),gpar.coltext=gpar(fontsize=8), gpar.rowtext=gpar(fontsize=8)) 


### final result 
grid.arrange(my_hist + opts(legend.position = "none"), arrangeGrob(legend, my_table), ncol = 2) 

enter image description here

+0

Cảm ơn bạn điều này rất hữu ích! – Elizabeth

+0

liên kết wiki đã chết – ZN13

+0

@ PajeetRamahari-Mawari-Kulmini Cảm ơn rất nhiều, nó thực sự hữu ích. Tôi đã cập nhật các liên kết. – dickoa

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