Two legends for polar ggplot (with one customized)

前端 未结 2 908
终归单人心
终归单人心 2021-02-10 12:55

Here is my data:

data <- structure(list(Indicator = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 6L, 6L, 6L, 6L, 4L, 4L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 
2L,          


        
2条回答
  •  终归单人心
    2021-02-10 13:49

    For a start, you may try something like this. You need to adjust arrangement and layout according to your own preferences.

    library(ggplot2)
    library(gridExtra)
    
    gg <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') + 
      geom_bar(width = 1, alpha=0.5, stat="identity") + 
      coord_polar()  + 
      theme(axis.ticks = element_blank()) + 
      facet_wrap(~Village, nrow=2, ncol = 3) 
    
    # create a table that translates legend.var to Variables 
    tbl <- unique(data[ , c("legend.var", "Variables")])
    
    # create a table grob 
    tt <- tableGrob(d = tbl,
                    col.just = "left",
                    gpar.coretext = gpar(col = "black", cex = 0.5),
                    gpar.coltext = gpar(col = "black", cex = 0.5, fontface = "bold"),
                    gpar.rowtext = gpar(col = "black", cex = 0.5, fontface = "italic"))
    
    # arrange plot and table grob
    grid.arrange(gg, tt, ncol = 2)
    

    update with a quick and dirty ggplot-only alternative

    # create labels
    labs <- with(tbl, paste(legend.var, Variables))
    
    gg <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator, col = Variables)) + 
      geom_bar(width = 1, alpha = 0.5, stat = "identity") +
      coord_polar()  + 
      theme(axis.ticks = element_blank()) + 
      facet_wrap(~ Village, nrow = 2, ncol = 3) +
      scale_color_grey(labels = labs, start = 0.8, end = 0.9)
    
    gg
    

    enter image description here

提交回复
热议问题