2017-06-06 58 views
2

我正嘗試使用gvisTreeMap創建樹圖。但我收到一個錯誤: 「gvisCheckTreeMapData(data,my.options)中的錯誤: parentvar和idvar不合適。」 我對R很新。任何人都可以知道如何去除錯誤並使其工作?RStudio TreeMap-Idvar不匹配Parentvar

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)) 

謝謝

德拉

回答

0

我遇到了同樣的問題!你需要做的是添加行,看起來像這樣:

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) 

導致數據幀df,看起來像這樣:

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 

(PS確保當您查看您的數據幀,又名View(df),該NA是「變灰」 - 否則,手動變量變成字符並將其重命名:df$Gender <- as.character(df$Gender); df$Gender[5] <- NA

這是必要的,因爲如果你看一下例子數據幀Regions(只需在您的控制檯中輸入Regions),您可以看到您需要一個「Parent」行。

> 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 

還記得改變idvar和parentvar到的因素:

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

現在,plot(Tree)作品! enter image description here

祝你好運!