R: ggvis - HTML function - failing to “add_tooltip

2019-05-31 13:44发布

this is a basic question about ggvis. I've googled, but still...

I want that, when someone hovers over a specific date (2014-12-05), the tooltip will show: 3,188 sessions (please, notice the comma).On so on for everyday on the data frame.

Have this df:

structure(list(date = structure(1:31, .Label = c("2014-12-01", 
"2014-12-02", "2014-12-03", "2014-12-04", "2014-12-05", "2014-12-06", 
"2014-12-07", "2014-12-08", "2014-12-09", "2014-12-10", "2014-12-11", 
"2014-12-12", "2014-12-13", "2014-12-14", "2014-12-15", "2014-12-16", 
"2014-12-17", "2014-12-18", "2014-12-19", "2014-12-20", "2014-12-21", 
"2014-12-22", "2014-12-23", "2014-12-24", "2014-12-25", "2014-12-26", 
"2014-12-27", "2014-12-28", "2014-12-29", "2014-12-30", "2014-12-31"
), class = "factor"), sessions = c(1932L, 1828L, 2349L, 8192L, 
3188L, 3277L, 2846L, 2541L, 5434L, 4290L, 2059L, 2080L, 2111L, 
3776L, 1989L, 1844L, 3641L, 1283L, 1362L, 1568L, 2882L, 1212L, 
957L, 851L, 928L, 1435L, 1115L, 1471L, 1128L, 1022L, 768L), id = 1:31), .Names = c("date", 
"sessions", "id"), row.names = c(NA, -31L), drop = TRUE, class = c("tbl_df", 
"tbl", "data.frame"))

And i need to get a Google Analytics look for sessions:

enter image description here

1) Want to plot a path graph, with a tool tip for every single day. 2) In the X Label, just want to plot the first date, the middle date, and the last date. See my code for my failed attempt.

My code:

EvolucionVisitas <- EvolucionVisitas %>% ggvis(~date, ~sessions) %>%
  add_tooltip(~sessions,"hover") %>%
  layer_paths()

I've read the help page, but i don't understand well the arguments. What is the html function?

The error says:

Warning: Unhandled error in observer: could not find function "html"
observe({
    value <- session$input[[id]]
    if (is.null(value)) 
        return()
    if (!is.list(value$data)) 
        return()
    df <- value$data
    class(df) <- "data.frame"
    attr(df, "row.names") <- .set_row_names(1L)
    fun(data = df, location = list(x = value$pagex, y = value$pagey), 
        session = session)
})

**How to show in the X label just the first, middle, and last dates?

After, @LyzandeR answer, i got what i needed, and added "Sessions: " to the tool tip. But have problems with the X label:

-This is my attempt to show just the first, middle, and last dates of the data frame:

EvolucionVisitas %>% ggvis(x= ~date, y= ~sessions, key := ~id) %>%
  layer_points()  %>%
  add_tooltip(mysessions ,"hover") %>%
  layer_paths() %>%
  add_axis("x",
           value=c(EvolucionVisitas$date[1], EvolucionVisitas$date[round(length(EvolucionVisitas$date)/2,0)],
                   tail(EvolucionVisitas$date, n=1)),
           properties=axis_props(
             labels=list(angle=90, fontSize = 10)))

标签: r ggvis
1条回答
一纸荒年 Trace。
2楼-- · 2019-05-31 14:48

If you see the documentation of ?add_tooltip you will see that the second argument needs to be a function and thus this is what you need to provide in the following way:

Data

df <- read.table(header=T, text='   date        sessions
2014-12-01      1932
2014-12-02      1828
2014-12-03      2349
2014-12-04      8192
2014-12-05      3188
2014-12-06      3277
2014-12-07      2846
2014-12-08      2541')

Solution

First of all you need to provide an id so that ggvis will know how to link the columns. I do this with the id column below:

df$id <- 1:nrow(df)

Then you need to create the function mysessions to enter into add_toolbox:

mysessions <- function(x) {
  if(is.null(x)) return(NULL)
  #notice below the id column is how ggvis can understand which session to show 
  row <- df[df$id == x$id, ]
  #prettyNum shows the number with thousand-comma separator  
  paste0(prettyNum(row$sessions, big.mark=",",scientific=F)) 
}

And this is how you write the function. Notice the key below:

library(ggvis)
df %>% ggvis(x= ~date, y= ~sessions, key := ~id) %>%
  layer_points()  %>%
  add_tooltip(mysessions ,"hover") %>%
  layer_paths()

Notice that add_tooltip does not work properly with layer_paths so you need to add a layer_paths on top of layer_points. The tooltip will work with layer_points.

I cannot show you everything here but here is a snapshot of how it shows when I hover my mouse pointer over the 3rd point:

enter image description here

查看更多
登录 后发表回答