Colouring points by factor within the margin of a faceted ggplot2 plot in R

后端 未结 2 1016
难免孤独
难免孤独 2020-12-31 17:26

I\'d like to create a faceted plot with margins in ggplot2. However, I\'d like the margin plot to have colours according to from which facet the particular point has been de

2条回答
  •  难免孤独
    2020-12-31 17:45

    Another option would be to create the faceted plots and margin plot separately and use the gridExtra library to combine them:

    library(ggplot2)
    library(gridExtra)
    mtcars$ALL <- "all"
    p <- ggplot(mtcars, aes(mpg, wt))
    p1 <- p + geom_point() + facet_grid(.~gear)
    p2 <- p + geom_point(aes(color=factor(gear))) + facet_grid(.~ALL)
    grid.arrange(p1, p2, ncol=2)
    

    enter image description here

提交回复
热议问题