2012-08-09 18 views
27

में किंवदंती के तहत एक तालिका डालना क्या grid.arrange() को split.screen() के रूप में कार्य करने के लिए वैसे भी है? मैं किंवदंती के नीचे सीधे स्थित एक टेबल व्यवस्थित करना चाहता हूं।एक 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) 

पैदा करता है:

enter image description here

लेकिन मैं इसे इस तरह मोटे तौर पर देखने के लिए करना चाहते हैं:

enter image description here

मैं split.screen() की कोशिश की लेकिन ऐसा प्रतीत नहीं होता ggplot प्रकार ग्राफिक्स के साथ काम करने के लिए। कोई सुझाव? धन्यवाद।

+0

चेक इस [कड़ी] (http://learnr.wordpress.com/2009/04/29/ggplot2-labelling-data-series-and-adding-a-data-table /) बाहर। मुझे थोड़ी देर पहले एक ही चीज़ करने की ज़रूरत थी, हालांकि मुझे यकीन नहीं है कि यहां कोड अब पुराना है या नहीं। –

+0

यह एक पुराना सवाल है, यदि आप उन्हें काम करना चाहते हैं तो आपको नीचे दिए गए उत्तरों में 'opts' को बदलना होगा। – durum

उत्तर

25

Dickoa का जवाब बहुत साफ है में कामयाब रहे। मेरा तत्व आपको तत्वों पर अधिक नियंत्रण देता है।

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

बहुत स्पष्ट ... और उत्पादन पर बहुत अच्छा नियंत्रण – dickoa

+0

@Iselzer आपकी सलाह के लिए धन्यवाद! बहुत सराहना की – Elizabeth

12

सबसे पहले आपको इस Wiki पर एक नज़र डालना चाहिए, कई उदाहरण हैं (व्यवस्था एक को देखें)। तो thoses उदाहरण का उपयोग करते हुए, मैं इस समाधान

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

धन्यवाद, यह बहुत उपयोगी था! – Elizabeth

+0

विकी लिंक मर चुका है – ZN13

+0

@ पायजीतरामारी-मावरी-कुलमिनी बहुत बहुत धन्यवाद, यह वास्तव में सहायक है। मैंने लिंक अपडेट किए। – dickoa