Two legends for polar ggplot (with one customized)

前端 未结 2 898
终归单人心
终归单人心 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:50

    Here is a solution using a gtable:

    library(ggplot2)
    p <- ggplot(data, aes(x = legend.var, y = score, fill = Indicator), color='black') + 
      geom_bar(width = 1, alpha=0.5, stat="identity") + 
      scale_y_continuous() + 
      coord_polar()  + 
      theme( axis.ticks = element_blank()) + 
      facet_wrap(~Village, nrow=2, ncol=3) + 
      guides(colour = guide_legend(title.hjust = 0.5)) +
      theme(legend.position=c(0.85,0.25))
    
    #create table
    library(gridExtra)
    tab <- tableGrob(unique(data[, c("legend.var", "Variables")]), 
                     show.rownames=FALSE, gpar.coretext=gpar(fontsize=10), 
                     gpar.coltext=gpar(fontsize=10, fontface='bold'),
                     gpar.corefill = gpar(fill = "grey90", col = "white"),
                     gpar.colfill = gpar(fill = "grey80", col = "white"))
    
    #arrange grobs
    library(gtable)
    a <- gtable(unit(c(0.7, 0.3) ,c("npc")), unit(1, "npc"))
    a <- gtable_add_grob(a, ggplotGrob(p),1,1)
    a <- gtable_add_grob(a, tab,1,2)
    
    #plot
    grid.draw(a)
    

    enter image description here

提交回复
热议问题