Ggplot2 equivalent to "factorization or categorization" in googleVis in R

Due to the static graph prepared by ggplot, we are migrating our graphs to googleVis with interactive diagrams. But when it comes to categorization, we face many problems. Let me give you an example to 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 a parameter such as alpha, colour, linetype, sizewhich we can use with categories such as the one shown below:

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

Not only a line chart, but most 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 the parameters to be changed or grouped by lines or diagrams.

Note: I have already tried dcastto make several columns based on the category column and use these several columns as input Y, but this is not what I would like to do.

Can anyone help me in this regard?

Let me know if you need more information.

+4
source share
1 answer

vrajs5 you are not alone! We struggled with this problem. In our case, we wanted to use fillhistograms, as in ggplot. This is the solution. You need to add the specially named columns associated with your variables in the data table for googleVis.

, , . Google ( !), , r.

@mages -, demo(googleVis):

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

, GOOGLEVIS

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

py.style , , .

myvar.googleVis_thing_youneed myvar googleVis.

(yvar = "py" ) GoogleVisBarChartBeforeRoleStyleExample

(yvar = c ( "py", "py.style" ))

GoogleVisBarChartRoleStyleExample

( Github), " " .

+2

Source: https://habr.com/ru/post/1541086/


All Articles