在R中的mosaicplot()中移动标签位置 [英] Moving label position in mosaicplot() in R

查看:263
本文介绍了在R中的mosaicplot()中移动标签位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出R中的示例镶嵌图,

Given this example mosaicplot in 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标签(类别名称),以便可以看到完整的名称?

How can I move the Category1 labels (edit: category names) upward so that the complete names are visible?

推荐答案

就像@MrFlick一样,我也可以看到标签.您更改了地块的边距吗?检查方法如下:

Like @MrFlick, I can also see the labels. Have you changed your plot margins? Here's how to check:

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

我粘贴了默认边距(c(底部,左侧,顶部,右侧)).如果您的尺寸较小,则可能没有留出标签的空间.要将它们重置为默认值(或所需的任何值),请执行par(mar=c(5,4,4,2)+0.1).

I've pasted in the default margins (c(bottom, left, top, right)). If yours are smaller, it might not leave room for the labels. To reset them to the defaults (or whatever you want) do par(mar=c(5,4,4,2)+0.1).

无论如何,如果您想移动标签,下面是一些示例:

In any case, if you want to move the labels around, here are some examples:

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标签:

UPDATE: To remove the axis labels, save the contingency table as an object and then set the dimnames attribute to NA. You can also, of course change or abbreviate the labels this way as well. For example, to remove the Category1 labels:

## 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 = "")

END UPDATE

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

You might also like the mosaic function in the vcd package. It's more complicated, but it gives you more control over the details of the plot. mosaic uses lattice rather than base graphics, so all the adjustments to the plot need to be done with lattice or grid, rather than the base graphics functions or parameters:

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))

有关很多示例,请参见此插图

See this vignette for lots of examples.

这篇关于在R中的mosaicplot()中移动标签位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆