spsully111
spsully111

Reputation: 1

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:

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'

My data set is as follows:

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

A 37 39 45 64

B 13 13 23 38

C 33 69 67 56

D 16 31 34 22

E 8 16 21 35

With Store ID in the first column and Age ranges in columns 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.

Upvotes: 0

Views: 1917

Answers (1)

MartineJ
MartineJ

Reputation: 795

Mosaic expects a table, not a dataframe. SandA is a dataframe. 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:

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)

Upvotes: 2

Related Questions