2012-02-22 40 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