Place a legend for each facet_wrap grid in ggplot2

前端 未结 4 856
闹比i
闹比i 2020-11-27 04:03

I have this data frame:

        Date Server FileSystem PercentUsed
1  12/1/2011      A          /          60
2   1/2/2012      A       /var          50
3            


        
4条回答
  •  半阙折子戏
    2020-11-27 04:22

    Instead of using facets, we could make a list of plots per group, then use cowplot::plot_grid for plotting. Each will have it's own legend:

    # make list of plots
    ggList <- lapply(split(x, x$Server), function(i) {
      ggplot(i, aes(Date, PercentUsed, group = 1, colour = FileSystem)) + 
        geom_jitter(size = 2) +
        geom_smooth(method = "loess", se = TRUE)})
    
    # plot as grid in 1 columns
    cowplot::plot_grid(plotlist = ggList, ncol = 1,
                       align = 'v', labels = levels(x$Server))
    

    As suggested by @Axeman, we could add labels using facet_grid(~Server), instead of labels = levels(x$Server).

提交回复
热议问题