2012-02-22 42 views
4

我正在使用qplot函數來生成直方圖。它生成好的情節,我對圖形很滿意。我也想打印直方圖數據,有沒有什麼辦法可以從qplot()找回那個返回對象?我正在使用hist()函數,如果我們添加選項plot = FALSE,則會給出數據,但是不會與qplot()一起使用。來自qplot的直方圖數據

+1

我相信這將有可能與ggplot2版本0.3,計劃於3月1日發佈到CRAN。 – Andrie 2012-02-22 07:57:10

+2

與此同時,最好明確指定休息時間(當你這樣做時,你顯然知道它們在哪裏)。 – 2012-02-22 13:20:13

+0

@Andrie 0.9,不是0.3 ;-p – kohske 2012-02-22 20:46:19

回答

0
library(gridExtra) 
library(gtable) 

fakeDF <- data.frame(group = sample(c('a', 'b', 'c', 'd'), 50, replace = T), 
       rand = sample(50:100, 50)) 

plot <- ggplot(fakeDF, aes(x = group, y = rand, group = group, fill = group)) + 
    geom_bar(stat = 'identity') 

table <- tableGrob(head(fakeDF)) 

grid.arrange(plot, 
      table, 
      ncol = 2) 

enter image description here

1

您可以使用功能ggplot_build()獲取用來製造ggplot()直方圖的實際數據。它們存儲在列表元素data中 - 條的中點在列x中,計數在列count中。

p<-ggplot_build(ggplot(movies,aes(x=rating))+geom_histogram()) 

head(p$data[[1]]) 
    y count x ndensity  ncount  density PANEL group ymin ymax xmin xmax 
1 0  0 0.75 0.00000000 0.00000000 0.000000000  1  1 0 0 0.6 0.9 
2 150 150 1.05 0.02967946 0.02967946 0.008505137  1  1 0 150 0.9 1.2 
3 122 122 1.35 0.02413930 0.02413930 0.006917512  1  1 0 122 1.2 1.5 
4 199 199 1.65 0.03937475 0.03937475 0.011283482  1  1 0 199 1.5 1.8 
5 366 366 1.95 0.07241789 0.07241789 0.020752535  1  1 0 366 1.8 2.1 
6 409 409 2.25 0.08092600 0.08092600 0.023190674  1  1 0 409 2.1 2.4