r - ggplot2 相当于 R 中 googleVis 中的 'factorization or categorization'

标签 r ggplot2 google-visualization googlevis

由于 ggplot 准备的静态图表,我们将图表转移到带有交互式图表的 googleVis。但是当涉及到分类时,我们面临着许多问题。让我举个例子来帮助你理解:

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

ggplot2 提供类似 alpha, colour, linetype, size 的参数我们可以将其与如下所示的类别一起使用:
ggplot(df) + geom_line(aes(x = x, y = y, colour = cat))

不仅仅是折线图,而且大多数 ggplot2 图都提供基于列值的分类。现在我想在 googleVis 中做同样的事情,基于值 df$cat我想改变参数或对折线或图表进行分组。

笔记:
我已经试过了 dcast根据类别列制作多列并将这些多列用作 Y 输入,但这不是我想做的。

任何人都可以帮我解决这个问题吗?

如果您需要更多信息,请与我们联系。

最佳答案

vrajs5 你并不孤单!我们在这个问题上挣扎。在我们的例子中,我们想要 fill像ggplot中的条形图。这就是解决方案。您需要添加特定命名的列,链接到您的变量 , 到您的数据表 googleVis捡起。

在我的填充示例中,这些被称为 角色 ,但是一旦您看到我的语法,您就可以将其抽象为注释和其他很酷的功能。谷歌都有documented here (check out superheroes example!)但它如何应用于 并不明显。 .

@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

看看 mages 示例(代码也在 Github 上),您将破解“基于列值的分类”问题。

关于r - ggplot2 相当于 R 中 googleVis 中的 'factorization or categorization',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23733904/

相关文章:

javascript - 向对象添加 Google Chart 功能

javascript - Google Charts 加载多列图表时出现问题

arrays - 按索引对切片 R 数组

在包函数中重定向/拦截函数调用

r - 这是 group_by 和 lead/lag 中的错误吗?

r - 如何在 ggplot geom_area 中强制执行堆栈排序

r - 使用 ggplot2 根据 y 值更改线条颜色

r - 如何增加 ggplot2 中的图例标题行间距?

从数据框的因素中删除引号

javascript - 更改谷歌可视化表日期格式