【问题标题】:ggplot2 equivalent of 'factorization or categorization' in googleVis in Rggplot2 相当于 R 中 googleVis 中的“分解或分类”
【发布时间】:2014-05-19 09:11:57
【问题描述】:

由于 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 输入,但这不是我想做的。

谁能帮我解决这个问题?

如果您需要更多信息,请告诉我。

【问题讨论】:

  • 我不熟悉 R,但在 Google Visualization API 的一般 javascript 实现中,所有数据系列都是由列定义的,而不是由列中的值定义的。您可能必须将数据拆分为多列才能获得所需的效果。
  • 这就是我提出这个问题的原因...在 R 中可能有一种方法...可能正在使用其他包?
  • 您可以使用plotly创建交互式ggplot图
  • @vrajs5 - 请查看使用 myvar.style 作为额外列发布到您的旧问题的解决方案,以从 googleVis 中获得更多信息!

标签: r ggplot2 google-visualization googlevis


【解决方案1】:

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

填充后的结果 (yvar = c("py", "py.style"))

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2011-05-05
    • 2013-04-12
    • 1970-01-01
    相关资源
    最近更新 更多