Histogram data from qplot

I use the qplot function to generate a histogram. It generates nice stories, and I am very pleased with the graphics. I also want to print the histogram data, is there any way to get this from qplot() of the return object? I used the hist() function, which gives data, if we add the option plot = FALSE , the same does not work with qplot() .

+4
source share
2 answers

You can use the ggplot_build() function to get the actual data used to create the ggplot() histogram. They are stored in the data list element - the midpoints for the columns are in the x column, and the numbers are in the count column.

  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 
+1
source
 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

0
source

Source: https://habr.com/ru/post/1397740/


All Articles