R中的马赛克图帮助

问题描述

我当前的情节:

enter image description here

我想要的情节(不要理会变量s)

enter image description here

具体来说:底部的解释变量为x轴,右侧为响应变量,相对频率为y,左侧为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)

解决方法

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

对于您来说,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)

default spine plot

就我个人而言,我会保留它。但是,如果从左到右切换轴标签确实很重要,反之亦然,则可以先取消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,y[1]) + y/2,labels = colnames(tab),tick = FALSE)

customized spine plot