Search code examples
rplotggplot2visualizationggvis

Exploded 180 degree pie chart in R ggplot or ggvis (image included)?


Given a dataset with a factor column (X1) and a subtotal column (X2)

  X1 X2 
1  1  12  
2  2  200 
3  3  23  
4  4  86  
5  5  141  

I would like to create a graphic like this:

like so

which gives x2 as a percentage of the X2 total, divided by X1.

Edit: clarity and adding dataset for reproducability


Solution

  • For example

    set.seed(1234)
    df <- data.frame(x = 1:6)
    df$y <- runif(nrow(df))
    df$type <- sample(letters, nrow(df))
    ggplot(df, aes(x+-.5, y, fill=type)) + 
      geom_bar(stat="identity", width=1) + 
      coord_polar(start = pi/2) + 
      scale_x_continuous(limits = c(0, nrow(df)*2)) + 
      geom_text(aes(label=scales::percent(y))) + 
      ggthemes::theme_map() + theme(legend.position = c(0,.15))
    

    gives you

    enter image description here