getting back histograms

15 views
Skip to first unread message

McCreight

unread,
Jan 13, 2011, 10:29:28 AM1/13/11
to ggplot2
Hi kind folks-

When i do something like the following example from the
geom_histogram() examples

m <- ggplot(movies, aes(x=rating)) + geom_histogram(binwidth = 0.5)
m + facet_wrap(~ mpaa)

I see what I want, the histograms for each mpaa, but how can I get
these histograms back into a data frame or a vector (like a table) so
that I can use them for other purposes?

Thanks,

James

Brandon Hurr

unread,
Jan 13, 2011, 10:39:40 AM1/13/11
to McCreight, ggplot2
What exactly are you trying to accomplish? Sounds a bit like you simply want to subset your data by the rating and then run some computations on individual subsets... 

Brandon


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Jan 13, 2011, 11:30:55 AM1/13/11
to James McCreight, ggplot2
Sounds like something Hadley, Baptiste, Takahashi, Dennis, or Thierry might be able to quickly solve. 

Seems to me like it would involve much more than one line of code. 

#Determine factor levels (levels)
#Subset data by factor (subset or [x == level[i]])
#Bin data for counts within factor (ddply?)
#create dataframe of counts by bin and factor (list, cbind, etc...)

Someone with inside knowledge could probably extract the pieces you need quickly from geom_histogram though...

B

On Thu, Jan 13, 2011 at 16:13, James McCreight <mccr...@gmail.com> wrote:
Hi Brandon-

There are several ways to look at this. Most plainly, I want the abscissa and ordinates of the plotted histograms... each one of them. They could be in individual vectors or in a data frame, i dont really care. The later would be more "slick", but is not necessary.

I could use table() or hist() but then I have to do all the clumsy subsetting. Why I'm approaching this through ggplot2 is because it clearly makes the histogram calculations over the facet group; a single command is both subsetting and generating the histograms. The syntax for achieving what i want is really easy, a one liner. Doing it manually via other methods seems to take way too much work. 

I'm sure there are certainly multiple ways to achieve this, I'm not tied to ggplot but if i could learn to get the coordinates I desire, that could be useful. I examined the m object and didnt discover what I was looking for.

James

Kohske Takahashi

unread,
Jan 14, 2011, 1:45:33 AM1/14/11
to Brandon Hurr, James McCreight, ggplot2
hi,

without warranty to work in future:

m <- ggplot(movies, aes(x=rating)) + geom_histogram(binwidth = 0.5)

ob <- m + facet_wrap(~ mpaa)
dp <- ggplot_build(ob)$data
dp[[1]][,]

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Hadley Wickham

unread,
Jan 14, 2011, 9:21:14 AM1/14/11
to McCreight, ggplot2

It's not hard to compute the essence of a histogram yourself:

movies$rating_bin <- round_any(movies$rating_bin, 0.5)
count(movies, c("rating_bin", "mpaa")

But for more detail it really depends on what you want to do with the results.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

James McCreight

unread,
Jan 14, 2011, 10:01:53 AM1/14/11
to Hadley Wickham, ggplot2
takahashi, very interesting to see this.
hadley, "count" was my missing link. 

thanks all. 

J
Reply all
Reply to author
Forward
0 new messages