Adding marginal histograms (or boxplots) to xyplot using lattice

久未见 提交于 2019-12-24 11:27:40

问题


Similar to this question, I'd like to to add marginal histograms or boxplots to a xyplot or densityplot using lattice. Is there a way to replace the right and/or top axes with these plots instead?

Something like:

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

How could I do this?


回答1:


Using ggplot2 with ggExtra works.

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"
)

See: https://daattali.com/shiny/ggExtra-ggMarginal-demo/



来源:https://stackoverflow.com/questions/31750561/adding-marginal-histograms-or-boxplots-to-xyplot-using-lattice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!