[英]Mosaic plot : empty cells and overlapping labels
I am using vcd library in R in order to perform a mosaic plot of 2 categorical variables, having multiple levels each. 我正在R中使用vcd库来执行2个类别变量的镶嵌图,每个类别变量具有多个级别。 I had to customize the function call "mosaic" in order to solve my problem of overlapping labels.
为了解决标签重叠的问题,我不得不自定义函数调用“ mosaic”。
However, I have 2 questions regarding this function mosaic : 但是,关于此功能镶嵌,我有两个问题:
> >
library(dplyr)
library(vcd)
library(MASS)
load(url("http://bit.ly/dasi_gss_data"))
study=dplyr::select(gss,year,homosex,vetyears)
study=filter(study, year>1974)
study=filter(study, year<1995 | year>2009)
study=group_by(study,year)
study_filt = filter(study,!all(is.na(homosex)) && !all(is.na(vetyears)))
study_filt <- as.data.frame(study_filt)
Simple call to the function mosaic 简单调用函数镶嵌
vcd::mosaic(data=study_filt,~homosex+vetyears)
Customized call to handle long level names 定制呼叫以处理长级别名称
vcd::mosaic(data=study_filt,~homosex+vetyears,
labeling=labeling_border(
rot_labels = c(90, 0, 90, 0),
just_labels=c("left","left","right","right"),
tl_varnames = FALSE,
gp_labels = gpar(fontsize = 9)))
I don't think that labeling_border
supports automatic line breaks, just rotation and abbreviation as standard strategies for long labels. 我不认为
labeling_border
支持自动换行符,只是旋转和缩写作为长标签的标准策略。
mosaic()
shows all levels(study_filt$homosex)
even if they have zero occurrences. mosaic()
显示所有levels(study_filt$homosex)
即使它们出现的次数为零。 This might be an important piece of information in some scenarios. 在某些情况下,这可能是重要的信息。 In your case it is easiest to drop the factor level by calling
factor()
another time: study_filt$homosex <- factor(study_filt$homosex)
在您的情况下,最简单的方法是通过再次调用
factor()
来降低因子水平: study_filt$homosex <- factor(study_filt$homosex)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.