11

これは私の入力データセットです:

> names(breakingbad.episodes)
[1] "season"           "episode"          "epnum"            "epid"             "title"           
[6] "url.trakt"        "firstaired.utc"   "id.tvdb"          "rating"           "votes"           
[11] "loved"            "hated"            "overview"         "firstaired.posix" "year"            
[16] "zrating.season"   "src"     

私のggvisでは、次の変数firstaired.posixとを使用していますrating

> str(breakingbad.episodes[c("firstaired.posix", "rating")])
'data.frame':   62 obs. of  2 variables:
$ firstaired.posix: POSIXct, format: "2008-01-21 02:00:00" "2008-01-28 02:00:00" "2008-02-  11 02:00:00" ...
$ rating          : num  87 85 84 84 83 90 87 85 88 83 ...

次のような情報ggvisを含むツールチップを使用して、正常に作成しました。rating

> breakingbad.episodes %>% 
ggvis(x = ~firstaired.posix, 
    y = ~rating, 
    fill = ~season) %>% 
layer_points() %>%
add_axis("x", title = "Airdate") %>%
add_axis("y", title = "Rating") %>%
add_legend("fill", title = "Season") %>%
add_tooltip(function(data){paste0("Rating: ", data$rating)}, "hover")

epidしかし、実際にはツールチップに変数などのより多くのデータを含めたいので、試しました:

…
add_tooltip(function(data){paste0("Rating: ", data$rating, "\n", "Epid: ", as.character(data$epid))}, "hover")

…使用as.character()理由epidは順序付けられた要因ですが、ツールチップの部分は空です。\n(挿入しようとしていた改行が欠落していることにも気付きましたが、それは別の問題です)。

この問題の原因はvis、データセットをパイプすることによって作成されたオブジェクトggvisに、表示したい情報が含まれていないようです。少なくともstr()、最初の例の出力を調べて収集したのはそのためです。

編集:私はその改行の問題を解決したので、私に指摘する必要はありません?add_tooltip- それを完全に忘れていました。

編集:ツールチップに任意の変数を配置することはできませんが、受け入れられた回答は正常に機能しています。これは、ユースケースに必要なものです。ありがとう! これが私が最終的にやったことです:

breakingbad.episodes <- transform(breakingbad.episodes, id = paste0(epid, " - ", title))

breakingbad.episodes %>% 
  ggvis(x = ~firstaired.posix, 
      y = ~rating, 
      fill = ~season, 
      key := ~id) %>% 
  layer_points() %>%
  add_axis("x", title = "Airdate") %>%
  add_axis("y", title = "Rating") %>%
  add_legend("fill", title = "Season") %>%
  add_tooltip(all_values, "click")
4

2 に答える 2

20

はい、可能です。通常、クライアントは実際にプロットにあるデータの列のみを返します。他の列を取得するには、キーを使用して元のデータにインデックスを付ける必要があります。これは単純で再現可能な例です

library(ggvis)
mtc <- mtcars
mtc$id <- 1:nrow(mtc)

all_values <- function(x) {
  if(is.null(x)) return(NULL)
  row <- mtc[mtc$id == x$id, ]
  paste0(names(row), ": ", format(row), collapse = "<br />")
}

mtc %>% ggvis(x = ~wt, y = ~mpg, key := ~id) %>%
  layer_points() %>%
  add_tooltip(all_values, "hover")
于 2014-07-02T09:54:07.017 に答える
2

回避策の 1 つは、プロパティに epid 変数を渡すことですkey。これは通常、遷移中にどの観測値が互いに対応しているかを追跡することを目的としていますが、ここでは副作用を生じることなくデータに epid を含めるという望ましい効果があります。

breakingbad.episodes <- data.frame(firstaired.posix = as.POSIXct(c("2008-01-21 02:00:00", "2008-01-28 02:00:00")),
rating = c(87, 85), epid = as.factor(c(12,23)), season = as.factor(c(1,2)), somevar = c("special", "very_special"))

breakingbad.episodes %>% 
  ggvis(x = ~firstaired.posix, 
        y = ~rating, 
        fill = ~season, key := ~epid) %>% 
  layer_points() %>%
  add_axis("x", title = "Airdate") %>%
  add_axis("y", title = "Rating") %>%
  add_legend("fill", title = "Season") %>%
  add_tooltip(function(data){paste0("Rating: ", data$rating, "\n", "Epid: ", as.character(data$epid))}, "hover")

元のデータセットから複数の変数を使用する必要がある場合は、id各行に一意の値を持つ列を追加してから、次のようにします。

breakingbad.episodes <- data.frame(id = c(1,2), firstaired.posix = as.POSIXct(c("2008-01-21 02:00:00", "2008-01-28 02:00:00")),
rating = c(87, 85), epid = as.factor(c(12,23)), season = as.factor(c(1,2)), somevar = c("special", "very_special"))

breakingbad.episodes %>% 
  ggvis(x = ~firstaired.posix, 
        y = ~rating, 
        fill = ~season, key := ~id) %>% 
  layer_points() %>%
  add_axis("x", title = "Airdate") %>%
  add_axis("y", title = "Rating") %>%
  add_legend("fill", title = "Season") %>%
  add_tooltip(function(data){paste0("Rating: ", data$rating, "\n", "Epid: ",
as.character(breakingbad.episodes$epid[breakingbad.episodes$id == data$id]), "\n", 
"What this is: ", breakingbad.episodes$somevar[breakingbad.episodes$id == data$id])}, "hover")
于 2014-07-02T07:34:03.267 に答える