Delaram Khayyatan
Delaram Khayyatan

Reputation: 31

RStudio TreeMap-Idvar does not match Parentvar

I am trying to create a Tree plot using gvisTreeMap. But I receive an Error: "Error in gvisCheckTreeMapData(data, my.options) : parentvar and idvar do not fit together." I am very new to R. Can anyone know how to remove the Error and make it work?

require(googleVis)
Gender = c("All", "All", "All", "All")
Beverage = c("Tea", "Soft", "Wine", "Water")
Under_50 = c(5, 10, 15, 50)
Up = c(10, 40, 70, 80)
df = data.frame(Beverage, Gender, Under_50, Up)
View(df)
Tree <- gvisTreeMap(df,  
                idvar= "Beverage", parentvar = "Gender", 
                sizevar = "Under_50", colorvar = "Up", 
                options=list(showScale=TRUE))

Thank you,

Dela

Upvotes: 2

Views: 664

Answers (1)

sweetmusicality
sweetmusicality

Reputation: 937

I ran into the same problem! What you need to do is add a row that looks like this:

df_add <- data.frame(Beverage = c("All"), Gender = c(NA), Under_50 = c(20), Up = c(20)) 
# I don't think it matters what values you put in the last two columns

df <- rbind(df, df_add)

resulting in a dataframe df that looks like this:

  Beverage Gender Under_50 Up
1      Tea    All        5 10
2     Soft    All       10 40
3     Wine    All       15 70
4    Water    All       50 80
5      All   <NA>       20 20

(P.S. make sure that when you view your dataframe, aka View(df), the NA is 'grayed out' - otherwise, change the variable into character manually and rename it: df$Gender <- as.character(df$Gender); df$Gender[5] <- NA)

This is needed because if you look at the example dataframe Regions (simply enter Regions in your console), you can see that you need one "Parent" row.

> Regions
    Region  Parent Val Fac
1   Global    <NA>  10   2
2  America  Global   2   4
3   Europe  Global  99  11
4     Asia  Global  10   8
5   France  Europe  71   2
6   Sweden  Europe  89   3
7  Germany  Europe  58  10
8   Mexico America   2   9
9      USA America  38  11
10   China    Asia   5   1
11   Japan    Asia  48  11

Also remember to change the idvar and parentvar into factors:

df$Beverage <- as.factor(df$Beverage)
df$Gender <- as.factor(df$Gender)

and now, plot(Tree) works! enter image description here

Best of luck!

Upvotes: 3

Related Questions