gVplot2相当于R中googleVis中的“因式分解或分类” [英] ggplot2 equivalent of 'factorization or categorization' in googleVis in R
问题描述
由于ggplot准备的静态图表,我们正在使用交互式图表将图表转换为GoogleVis。但是当涉及到分类时,我们正面临许多问题。让我举个例子来帮助你理解:
#dataframe
df = data.frame(x = sample( 1 = 100),y =样本(1:100),cat =样本(c('a','b','c'),100,replace = TRUE))
ggplot2提供像 alpha,color,linetype,size
这样的参数,如下所示:
ggplot(df)+ geom_line(aes(x = x,y = y,color = cat))
不仅仅是折线图,但大多数ggplot2图提供了基于列值的分类。现在我想根据值 df $ cat
在googleVis中执行相同的操作。我希望参数可以更改或分组线或图表。
注意:
我已经尝试过使用 dcast
来创建基于类别列的多列,并将这些多列用作Y输入,但这不是我想要做的。
任何人都可以帮助我解决这个问题吗?
让我知道您是否需要更多信息。
我们在这个问题上挣扎。在我们的例子中,我们希望像 googleVis
的数据表中以提取。 在我的填充示例中,这些被称为角色,但是一旦您看到我的语法,就可以将其抽象为注释和其他很酷的功能。 Google提供了),您将破解基于列值的分类问题。
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 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 r.
@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.
这篇关于gVplot2相当于R中googleVis中的“因式分解或分类”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!