1

鉴于 R 中的这个示例马赛克图,

## create example data frame
set.seed(56)
df1 <- data.frame(Category1 = rep(c("Category name", "Longer category name", "Cat name"), times = c(42, 19, 6)), Category2 = sample(c("Low", "Mid", "High"), 67, replace =T, prob = c(0.25, 0.40, 0.35)))

df1

## make a contingency table
table(df1)

## make the mosaic plot
mosaicplot(table(df1), color = 1:3, las = 2, ylab = "Category2", xlab = "Category1", main = "")

如何向上移动 Category1 标签(编辑:类别名称)以使完整名称可见?

4

1 回答 1

1

像@MrFlick 一样,我也可以看到标签。你有没有改变你的情节边缘?以下是检查方法:

par("mar")
[1] 5.1 4.1 4.1 2.1

我已经粘贴了默认边距(c(下,左,上,右))。如果您的较小,则可能不会为标签留出空间。要将它们重置为默认值(或任何您想要的),请执行par(mar=c(5,4,4,2)+0.1).

无论如何,如果你想移动标签,这里有一些例子:

mosaicplot(table(df1), color = 1:3, las = 1, main = "", xlab="", ylab="")
mtext(side = 1, "Category1", line = 0.5, col="green")
mtext(side = 1, "Category1", line = 1, col="blue")
mtext(side = 1, "Category1", line = 2, col="red")
mtext(side = 2, "Category2", line = -1, col="purple")

在此处输入图像描述

更新:要删除轴标签,请将列联表另存为对象,然后将dimnames属性设置为NA. 当然,您也可以通过这种方式更改或缩写标签。例如,要删除Category1标签:

## make a contingency table
tab1 = table(df1)
dimnames(tab1)[["Category1"]] = rep(NA, length(unique(df1$Category1)))

## make the mosaic plot
mosaicplot(tab1, color = 1:3, las = 2, ylab = "Category2", 
           xlab = "Category1", main = "")

结束更新

您可能还喜欢包mosaic中的功能vcd。它更复杂,但它可以让您更好地控制情节的细节。mosaic使用lattice而不是基本图形,因此对绘图的所有调整都需要使用latticeor完成grid,而不是基本图形函数或参数:

library(vcd)
mosaic(table(df1), color = 1:3, las = 2, ylab = "Category2", 
       xlab = "Category1", main = "", 
       labeling_args = list(offset_varnames = c(left = 2, top=0)),
       gp = gpar(fill = 1:3))

有关大量示例,请参阅此小插图

于 2014-09-26T04:05:52.183 回答