4

Due to static graph prepared by ggplot, we are shifting our graphs to googleVis with interactive charts. But when it comes to categorization we are facing many problems. Let me give example which will help you understand:

#dataframe
df = data.frame( x = sample(1:100), y = sample(1:100), cat = sample(c('a','b','c'), 100, replace=TRUE) )

ggplot2 provides parameter like alpha, colour, linetype, size which we can use with categories like shown below:

ggplot(df) + geom_line(aes(x = x, y = y, colour = cat))

Not just line chart, but majority of ggplot2 graphs provide categorization based on column values. Now I would like to do the same in googleVis, based on value df$cat I would like parameters to get changed or grouping of line or charts.

Note: I have already tried dcast to make multiple columns based on category column and use those multiple columns as Y input, but that it not what I would like to do.

Can anyone help me regarding this?

Let me know if you need more information.

vrajs5
  • 4,066
  • 1
  • 27
  • 44
  • I'm not familair with R, but in the general javascript implementation of the Google Visualization API, all data series are defined by columns, not by values in a column. You will likely have to split your data into multiple columns to get the effect you want. – asgallant May 19 '14 at 17:28
  • Thats why I have put this question... There might be a way around in R... May be using other package? – vrajs5 Jun 01 '14 at 06:04
  • You can create interactive ggplot graphs with [plotly](http://ropensci.org/blog/2014/04/17/plotly/) – David Arenburg Jun 01 '14 at 07:08
  • @vrajs5 -please take a look at the solution posted to your old question using `myvar.style` as extra column to get more out of googleVis! – micstr May 29 '15 at 06:27

1 Answers1

2

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

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

GoogleVisBarChartRoleStyleExample

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

micstr
  • 5,080
  • 8
  • 48
  • 76