动画时gvisMotionChart为空白

时间:2017-09-07 19:05:37

标签: r google-visualization googlevis

问题:

我的gvisMotionChart在暂停时工作正常,但在动画时为空。

代码:

Motion=gvisMotionChart(mydat,
                       idvar="Time_Period_Year_Cd",
                       timevar="Year",
                       xvar="Total_Customers",
                       yvar="Percent_Happy",
                       colorvar = "New_Product_Count",
                       sizevar = "Group_A_Count"
)
plot(Motion)

结果(暂停时):

enter image description here

结果(动画时):

enter image description here

我认为它必须是我的代码或数据的东西,因为我能够让文档中的示例正常工作。我尝试了几种变体,比如没有指定xvar然后将“年”绘制为x轴等等。

数据:

mydat <- structure(list(Time_Period_Year_Cd = c(201220L, 201320L, 201340L, 
201360L, 201420L, 201440L, 201460L, 201480L, 201520L, 201540L, 
201560L, 201580L, 201620L, 201640L, 201660L, 201680L, 201720L
), New_Product_Count = c(1606L, 1834L, 1205L, 1204L, 1645L, 704L, 
651L, 473L, 692L, 559L, 535L, 531L, 911L, 663L, 599L, 702L, 512L
), Group_A_Count = c(616, 670, 512, 520, 594, 265, 215, 148, 
235, 171, 160, 166, 231, 220, 148, 138, 101), Group_B_Count = c(267, 
288, 177, 194, 320, 122, 156, 103, 121, 108, 105, 105, 187, 146, 
134, 152, 103), Group_C_Count = c(365, 420, 293, 269, 373, 172, 
151, 120, 192, 132, 135, 148, 225, 150, 191, 205, 177), Group_D_Count = c(333, 
429, 202, 204, 335, 132, 121, 97, 133, 143, 131, 107, 264, 139, 
119, 196, 129), Number_Bought_Per_Customer = c(5.46637608966376, 
6.4432933478735, 6.79668049792531, 7.04734219269103, 7.2468085106383, 
7.41193181818182, 7.44086021505376, 6.48625792811839, 6.91329479768786, 
7.16994633273703, 6.49906542056075, 5.30885122410546, 4.78155872667398, 
4.09049773755656, 3.80801335559265, 3.04415954415954, 2.826171875
), Total_Customers = c(5038L, 5940L, 5557L, 5472L, 6052L, 5164L, 
4544L, 3954L, 4473L, 4948L, 3884L, 3723L, 4011L, 4303L, 3413L, 
3421L, 2964L), Percent_Happy = c(0.797988105101756, 0.83794901700776, 
0.773512106024391, 0.775157532067893, 0.834237370911927, 0.8306291015089, 
0.820150552373225, 0.824696031621165, 0.776615269241095, 0.848073917652629, 
0.841092657179119, 0.781823675677749, 0.840457049668049, 0.763698900181159, 
0.872781703430453, 0.896473511416122, 0.787873482140602), Year = c(2012, 
2013, 2013, 2013, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 
2016, 2016, 2016, 2016, 2017)), .Names = c("Time_Period_Year_Cd", 
"New_Product_Count", "Group_A_Count", "Group_B_Count", "Group_C_Count", 
"Group_D_Count", "Number_Bought_Per_Customer", "Total_Customers", 
"Percent_Happy", "Year"), row.names = c(NA, 17L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

每个idvar值只有一个timevar值:

mydat %>% count(Time_Period_Year_Cd) %>% head
#   Time_Period_Year_Cd     n
#                 <int> <int>
# 1              201220     1
# 2              201320     1
# 3              201340     1
# 4              201360     1
# 5              201420     1
# 6              201440     1

所以不可能有任何过渡。与例如相反。

df <- mydat %>% mutate(idvar = substr(Time_Period_Year_Cd, 1, 4)) %>% group_by(idvar) %>% mutate(timevar = 1:n()) %>% ungroup
Motion=gvisMotionChart(df,
                       idvar="idvar",
                       timevar="timevar",
                       xvar="Total_Customers",
                       yvar="Percent_Happy",
                       colorvar = "New_Product_Count",
                       sizevar = "Group_A_Count"
)
plot(Motion)