【问题标题】:Adding marginal histograms (or boxplots) to xyplot using lattice使用格子将边际直方图(或箱线图)添加到 xyplot
【发布时间】:2015-07-31 16:06:04
【问题描述】:

类似于this 问题,我想使用lattice 将边缘直方图或箱线图添加到xyplotdensityplot。有没有办法用这些图代替右轴和/或上轴?

类似:

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

我该怎么做?

【问题讨论】:

  • 我不认为“如果我可以使用这样的函数会很好:automatic_awesome_plot(x)”实际上显示了很多努力
  • 我真的只是想问是否可以用lattice 中的任意边缘图替换轴。在不知道更多关于如何在lattice 中绘制轴的情况下,我没有太多工作要做,如果根本不可能,我宁愿在投入大量时间学习轴功能的内部机制之前知道这一点。使用panel.plots 的解决方案,这是我通常开始的地方,在这里似乎不合适。
  • 格子图也是网格图,因此使用gridExtra 仍然是布局图的一种选择。标准xyplot 函数内部没有用于在边缘添加绘图的机制。你需要做很多 grob 重新安排自己。

标签: r histogram scatter-plot lattice boxplot


【解决方案1】:

使用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/

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    相关资源
    最近更新 更多