library(vcd) ################################################## ## Hair/Eye Color ################################################## HairEyeColor ?HairEyeColor str(HairEyeColor) ## table to df: as.data.frame df <- as.data.frame(HairEyeColor) df ## df to table: xtabs tab <- xtabs(Freq ~ ., data = df) tab ## structable & mosaic plot: split horiz./vert./horiz./vert./etc. ## don't use for more than four variables ## Convenient layout of table: structable(~ Hair + Eye, data = df) ## or structable(~ Hair + Eye + Sex, data = df) ## Mosaic plot without independence test - area is porportional to cell entry mosaic(~ Hair + Eye, data = df, shade = FALSE) mosaic(~ Hair + Eye + Sex, data = df, shade = FALSE) ## Mosaic plot with independence test mosaic(~ Hair + Eye, data = df, shade = TRUE) ## The hyp. "Haircolor and Eyecolor are independent" is clearly rejected (p-value < 2.22*10^-16). ## If you have black hair, it is "surprisingly" likely that you have brown eyes and surprisingly unlikely to have blue eyes. ## If you have blond hair, you are surprisingly unlikely to have brown eyes and surprisingly likely to have blue eyes. ## Make a mosaic plot for each sex: cotabplot(~ Hair + Eye | Sex, data = df, shade = TRUE) ## The connection blue eyes - blond hair in women is much more striking than in men