简体   繁体   English

马赛克图:空单元格和重叠标签

[英]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 : 但是,关于此功能镶嵌,我有两个问题:

  1. Why proper line break for long level names is not done automatically, when the simple call is done ? 简单调用完成后,为什么没有自动为长级别名称换行? By looking at various vcd tutorial, it looks the function by default should handle that. 通过查看各种vcd教程,它看起来是默认情况下应该处理的功能。
  2. Why vcd is plotting empty cells ? 为什么VCD正在绘制空单元格? For example, in my dataframe study_filt, I have no observation with the homosex variable set to "Other". 例如,在我的数据框study_filt中,同性恋变量设置为“其他”时,我没有观察到。 Can it be removed ? 可以将其删除吗?

> >

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)))
  1. I don't think that labeling_border supports automatic line breaks, just rotation and abbreviation as standard strategies for long labels. 我不认为labeling_border支持自动换行符,只是旋转和缩写作为长标签的标准策略。

  2. 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.

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