Combination Boxplot and Histogram using ggplot2

后端 未结 4 1379
不知归路
不知归路 2020-12-08 01:04

I am trying to combine a histogram and boxplot for visualizing a continuous variable. Here is the code I have so far

require(ggplot2)
require(gridExtra)
p1 =         


        
4条回答
  •  星月不相逢
    2020-12-08 01:36

    The best solution I know is using the ggpubr package:

    require(ggplot2)
    require(ggpubr)
    p1 = qplot(x = 1, y = mpg, data = mtcars, xlab = "", geom = 'boxplot') + 
         coord_flip()
    p2 = qplot(x = mpg, data = mtcars, geom = 'histogram')
    ggarrange(p2, p1, heights = c(2, 1), align = "hv", ncol = 1, nrow = 2)
    

提交回复
热议问题