【问题标题】:Mosaic Plot Help in RR中的马赛克图帮助
【发布时间】:2021-01-09 05:19:20
【问题描述】:

我目前的情节: 我想要的情节(不要介意变量s)

具体来说:解释变量位于底部,x 轴位于右侧,响应变量位于右侧,相对频率位于左侧,y 轴位于左侧。我将在下面附上我的 R 代码。

mosaictable <- matrix (c (3, 9, 22, 21), byrow = T, ncol = 2)
rownames (mosaictable) = c ("White", "Blue ")
colnames (mosaictable) = c ("Captured", "Not Captured")
mosaicplot  ((mosaictable), sub = "Pigeon Color", ylab = "Relative frequency", 
            col = c ("firebrick", "goldenrod1"), font = 2, main = "Mosaic Plot of Pigeon Color and Their Capture Rate"
            
            )
axis (1)
axis (4)

【问题讨论】:

    标签: r mosaic-plot


    【解决方案1】:

    这种特殊风格的马赛克显示,您在 y 轴上有一个“因”变量并想要添加相应的注释,有时也称为“脊柱图”。 R 在spineplot() 函数中实现了这一点。当yx 都是分类时,plot(y ~ x) 也会在内部调用spineplot()

    在您的情况下,spineplot() 几乎可以自动执行您想要的所有操作,前提是您为其提供格式良好的 "table" 对象:

    tab <- as.table(matrix(c(3, 22, 9, 21), ncol = 2))
    dimnames(tab) <- list(
      "Pigeon Color" = c("White", "Blue"),
      "Relative Frequency" = c("Captured", "Not Captured")
    )
    tab
    ##             Relative Frequency
    ## Pigeon Color Captured Not Captured
    ##        White        3            9
    ##        Blue        22           21
    

    然后你得到:

    spineplot(tab)
    

    就我个人而言,我会把它留在那里。但是,如果从左到右切换轴标签非常重要,反之亦然,那么您可以通过首先抑制axes = FALSE 然后手动添加它们来实现。其坐标需要分别从第一个变量的边际分布和给定第一个变量的第二个变量的条件分布中获得

    x <- prop.table(margin.table(tab, 1))
    y <- prop.table(tab, 1)[2, ]
    spineplot(tab, col = c("firebrick", "goldenrod1"), axes = FALSE)
    axis(1, at = c(0, x[1]) + x/2, labels = rownames(tab), tick = FALSE)
    axis(2)
    axis(4, at = c(0, y[1]) + y/2, labels = colnames(tab), tick = FALSE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2012-08-28
      • 2017-09-23
      • 2023-01-20
      • 2012-08-19
      • 2022-08-24
      相关资源
      最近更新 更多