简体   繁体   English

R 中的马赛克图帮助

[英]Mosaic Plot Help in R

My current plot:我目前的情节: 在此处输入图片说明 My desired plot (nevermind the variables s)我想要的情节(别管变量s)

在此处输入图片说明

Specifically: explanatory variables on the bottom with an x-axis, response variables on the right, relative frequency and the y-axis on the left .具体来说:底部为 x 轴的解释变量,右侧的响应变量,相对频率和左侧的 y 轴 I'll attach my R code below.我将在下面附上我的 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)

This particular flavor of mosaic display where you have a "dependent" variable on the y-axis and want to add corresponding annotation, is sometimes also called a "spine plot".这种特殊风格的马赛克显示,您在 y 轴上有一个“因”变量并想要添加相应的注释,有时也称为“脊椎图”。 R implements this in the spineplot() function. R 在spineplot()函数中实现了这一点。 Also plot(y ~ x) internally calls spineplot() when both y and x are categorical.yx都是分类时, plot(y ~ x) spineplot()在内部调用spineplot()

In your case, spineplot() does almost everything you want automatically provided that you supply it with a nicely formatted "table" object:在您的情况下, 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

And then you get:然后你得到:

spineplot(tab)

默认脊椎图

Personally, I would leave it at that.就个人而言,我会离开它。 But if it is really important to switch the axis labels from left to right and vice versa, then you can do so by first suppressing axes = FALSE and then adding them manually afterwards.但是,如果从左到右切换轴标签真的很重要,反之亦然,那么您可以通过首先抑制axes = FALSE然后手动添加它们来实现。 The coordinates for that need to be obtained from the marginal distribution of the first variable and the conditional distribution of the second variable given the first, respectively坐标需要分别从第一个变量的边际分布和给定第一个变量的第二个变量的条件分布中获得

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)

自定义脊椎图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM