views:

242

answers:

1

A colleague of mine needs to plot 101 bull's-eye charts. This is not her idea. Rather than have her slave away in Excel or God knows what making these things, I offered to do them in R; mapping a bar plot to polar coordinates to make a bull's-eye is a breeze in ggplot2.

I'm running into a problem, however: the data is already aggregated, so Hadley's example here isn't working for me. I could expand the counts out into a factor to do this, but I feel like there's a better way - some way to tell the geom_bar how to read the data.

The data looks like this:

    Zoo Animals Bears Polar Bears
1 Omaha      50    10           3

I'll be making a plot for each zoo - but that part I can manage.

and here's its dput:

structure(list(Zoo = "Omaha", Animals = "50", Bears = "10", `Polar Bears` = "3"), .Names = c("Zoo", 
"Animals", "Bears", "Polar Bears"), row.names = c(NA, -1L), class = "data.frame")

Note: it is significant that Animals >= Bears >= Polar Bears. Also, she's out of town, so I can't just get the raw data from her (if there was ever a big file, anyway).

Thanks for your help.

+2  A: 

While we're waiting for a better answer, I figured I should post the (suboptimal) solution you mentioned. dat is the structure included in your question.

d <- data.frame(animal=factor(sapply(list(dat[2:length(dat)]),
                function(x) rep(names(x),x))))
cxc <- ggplot(d, aes(x = animal)) +  geom_bar(width = 1, colour = "black") 
cxc + coord_polar()
Christopher DuBois
Thanks, Chris! That's very close to what I'd put together, except I had explicitly spelled out the rep() for each group. sapply() is still like magic to me.
Matt Parker
This does, in fact, appear to be the best answer.
Matt Parker