使用晶格将边缘直方图(或箱线图)添加到xyplot

时间:2015-07-31 16:06:04

标签: r histogram scatter-plot lattice boxplot

this问题类似,我想使用xyplot将边缘直方图或箱图添加到densityplotlattice。有没有办法用这些图替换右轴和/或顶轴?

类似的东西:

library(lattice)
x <- rnorm(100)
y <- rnorm(100)
xyplot(x~y, 
       x.top  = histogram(~x), # desired 
       y.right = bwplot(~y)    # desired
       )

我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

ggplot2ggExtra一起使用。

library(ggplot2)
library(ggExtra)

p <- ggplot(cars, aes_string('speed', 'dist')) +
  geom_point() + theme_bw(15)

ggExtra::ggMarginal(
  p,
  type = 'boxplot',
  margins = 'both',
  size = 5,
  color = "black",
  fill  = "darkgrey"
)

请参阅:https://daattali.com/shiny/ggExtra-ggMarginal-demo/