简体   繁体   English

R中的马赛克图错误

[英]Mosaic Plot Error in R

I am new to R, and I'm trying to generate a mosaic plot using the VCD package in R but my code generates the following error: 我是R的新手,我试图使用R中的VCD包生成镶嵌图,但是我的代码生成以下错误:

Setting row names on a tibble is deprecated. 不建议在小标题上设置行名。

Error in loglin(x, expected, fit = TRUE, print = FALSE) : (list) object cannot >be coerced to type 'double' loglin中的错误(x,预期,fit = TRUE,print = FALSE):(list)对象不能>强制键入'double'

My data set is as follows: 我的数据集如下:

Store 16-24 25-34 35-49 50+ 商店16-24 25-34 35-49 50+

A 37 39 45 64 A 37 39 45 64

B 13 13 23 38 B 13 13 23 38

C 33 69 67 56 C 33 69 67 56

D 16 31 34 22 D 16 31 34 22

E 8 16 21 35 电子邮件8 16 21 35

With Store ID in the first column and Age ranges in columns 2-4. 第一列中的商店ID,而第2-4列中的年龄范围。

My code to generate the mosaic plot is: 我生成镶嵌图的代码是:

library(readr)
SandA = readr::read_csv("StoresAndAges.csv", col_names = TRUE)
SandA
library(vcd)
mosaic(SandA, shade=TRUE, legend=TRUE)

I am brand new to R, so any help pointing me in the right direction is appreciated. 我是R的新手,因此向我指示正确方向的任何帮助都将受到赞赏。

Mosaic expects a table, not a dataframe. Mosaic需要一个表,而不是一个数据框。 SandA is a dataframe. SandA是一个数据SandA The contents look like a table, but it is not. 内容看起来像一张桌子,但事实并非如此。 When you have a My_df with columns Store and Age_Range , and rows filled with the appropriate content, one row per observation, you can do this to obtain the mosaicplot: 当您有一个My_df且具有StoreAge_Range列,并且行中填充了适当的内容时,每个观察值一行,您可以这样做以获得镶嵌图:

mosaic(table(My_df$Store, My_df$Age_Range))

Or, in separate steps: 或者,在单独的步骤中:

# first make a table that looks like your original data
My_table <-table(My_df$Store, My_df$Age_Range)
# My_table is a table, so it can be fed to mosaic()
mosaic(My_table)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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