Search code examples
rpie-chartfacet-wrap

create pie charts with facet wrap?


I manage to create a dataframe as shown but I dont know how to create faceted pie charts out of it. Can someone show me how? Thanks in advance. enter image description here


Solution

  • I am not sure what you want to show, so you could do something like this:

    df <- data.frame(rideable_type = c("classic_bike", "classic_bike", "docked_bike", "electric_bike", "electric_bike"),
                     member_causal = c("causal", "member", "causal", "causal", "member"),
                     number_of_bikes = c(1313878, 2013712, 308241, 1048194, 1250025),
                     total = c(2670303, 3263737, 2670303, 2670303, 3263737),
                     freq = c(49.2, 61.6, 11.5, 39.2, 38.3))
    
    
    library(ggplot2)
    ggplot(df, aes(x = factor(1), fill=factor(rideable_type))) + 
      facet_wrap(~member_causal) + 
      geom_bar(width = 1,position = "fill") + 
      coord_polar(theta="y") +
      labs(fill = "rideable type", x = "", y = "") 
    

    Created on 2022-07-08 by the reprex package (v2.0.1)