ggplot2 equivalent of 'factorization or categorization' in googleVis in R

岁酱吖の 提交于 2019-11-29 07:51:49

vrajs5 you are not alone! We struggled with this issue. In our case we wanted to fill bar charts like in ggplot. This is the solution. You need to add specifically named columns, linked to your variables, to your data table for googleVis to pick up.

In my fill example, these are called roles, but once you see my syntax you can abstract it to annotations and other cool features. Google has them all documented here (check out superheroes example!) but it was not obvious how it applied to .

@mages has this documented on this webpage, which shows features not in demo(googleVis):

http://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html

EXAMPLE ADDING NEW DIMENSIONS TO GOOGLEVIS CHARTS

# in this case
# How do we fill a bar chart showing bars depend on another variable? 
#   We wanted to show C in a different fill to other assets

suppressPackageStartupMessages(library(googleVis))
library(data.table) # You can use data frames if you don't like DT

test.dt  = data.table(px = c("A","B","C"), py = c(1,4,9),
                      "py.style" = c('silver', 'silver', 'gold'))

# Add your modifier to your chart as a new variable e.g. py1.style
test <-gvisBarChart(test.dt, 
                    xvar = "px",
                    yvar = c("py", "py.style"),
                    options = list(legend = 'none'))
plot(test)

We have shown py.style deterministically here, but you could code it to be dependent on your categories.

The secret is myvar.googleVis_thing_youneed linking the variable myvar to the googleVis feature.

RESULT BEFORE FILL (yvar = "py")

RESULT AFTER FILL (yvar = c("py", "py.style"))

Take a look at mages examples (code also on Github) and you will have cracked the "categorization based on column values" issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!