ggplot2: multiple plots in a single row with a single legend

前端 未结 2 2030
盖世英雄少女心
盖世英雄少女心 2021-01-04 20:46

I want a combined plot of two plots + their legend like this:

library(ggplot2) 
library(grid)
library(gridExtra)
dsamp <- diamonds[sample(nrow(diamonds),          


        
2条回答
  •  日久生厌
    2021-01-04 20:50

    Why don't you use facetting?

    library(reshape2)
    dmelt <- melt(dsamp, id.vars = c("price", "clarity"), measure.vars = c("carat", "depth"))
    ggplot(dmelt, aes(x = price, y = value, color = clarity)) +
      geom_point() +
      facet_wrap(~ variable, scales = "free")
    

提交回复
热议问题