Search code examples
rggvis

Add Date Tooltip to GGvis object R


I want to add to a plot with Date axis (x axis) a tooltip that will include the current date, currently the tooltip shows the numeric value of the date, instead of the date format

the Data:

> dput(for_plot)
structure(list(ext_install_date = structure(c(16638, 16660, 16700, 
16710, 16712, 16729, 16730, 16736, 16752, 16768, 16717, 16755, 
16756, 16757, 16758, 16662, 16750, 16769, 16785, 16665, 16699, 
16632, 16682, 16683, 16684, 16698, 16634, 16679, 16720, 16754, 
16701, 16718, 16685, 16687, 16774, 16775, 16647, 16680, 16596, 
16630, 16721, 16725, 16703, 16706, 16723, 16726, 16650, 16651, 
16686, 16707, 16771, 16722, 16739, 16760, 16794, 16724, 16742, 
16777, 16648, 16653, 16670, 16744, 16676, 16636, 16671, 16654, 
16740, 16746, 16763, 16642, 16728, 16733, 16743, 16765, 16692, 
16689, 16709, 16711, 16735, 16639, 16695, 16780, 16784, 16640, 
16644, 16645, 16731, 16643, 16714, 16659, 16753, 16786, 16657, 
16737, 16663, 16749, 16702, 16719, 16770, 16751, 16776, 16681, 
16631, 16789, 16674, 16727, 16772, 16762, 16795, 16778, 16649, 
16672, 16766, 16655, 16741, 16764, 16693, 16747, 16781, 16732, 
16715, 16803, 16787, 16658, 16696, 16664, 16646, 16626, 16629, 
16790, 16773, 16652, 16796, 16667, 16673, 16759, 16656, 16748, 
16637, 16708, 16691, 16641, 16799, 16697, 16782, 16688, 16716, 
16804, 16788, 16627, 16633, 16791, 16797, 16668, 16800, 16783, 
16734, 16677, 16666, 16704, 16628, 16792, 16798, 16669, 16675, 
16761, 16767, 16678, 16661, 16738, 16635, 16793, 16745, 16694, 
16779, 16801, 16802, 16590, 16591, 16593, 16617, 16618, 16619, 
16589, 16592, 16608, 16610, 16611, 16624, 16623, 16595, 16713, 
16594, 16615, 16609, 16601, 16616, 16597, 16602, 16598, 16599, 
16605, 16600, 16620, 16606, 16622, 16588, 16603, 16604, 16607, 
16705, 16625, 16690, NA, 16613, 16612, 16621, 16587, 16614), class = "Date"), 
    sum = c(42133989L, 55439830L, 12474088L, 16782989L, 12498558L, 
    23097594L, 18694459L, 18613591L, 17164639L, 10998479L, 17434372L, 
    16134588L, 15765093L, 14780377L, 12238314L, 60259367L, 14299153L, 
    9228893L, 7181921L, 57044570L, 12671147L, 34528083L, 28685034L, 
    27538625L, 23861138L, 12939897L, 34804466L, 27256411L, 15470425L, 
    17683150L, 13919150L, 17683908L, 24785274L, 17297336L, 10883551L, 
    10765214L, 52309168L, 32311372L, 33612420L, 30994837L, 17528116L, 
    17979427L, 12904065L, 14452388L, 16659048L, 24373216L, 50197552L, 
    64194782L, 18635738L, 12370060L, 10714545L, 17080052L, 17320119L, 
    11792389L, 4250804L, 17337770L, 16257833L, 10389087L, 55407448L, 
    60149494L, 52791093L, 16748038L, 26324948L, 35491474L, 39187635L, 
    58805623L, 17361957L, 15002820L, 11834933L, 42022359L, 18834611L, 
    18499567L, 15771667L, 12734880L, 9901353L, 11215141L, 12242264L, 
    13603119L, 17919976L, 41100805L, 14435765L, 9727462L, 7604216L, 
    41830337L, 48193262L, 53128495L, 19248325L, 43074450L, 11930683L, 
    57259190L, 17360447L, 6925452L, 49781307L, 17483336L, 60223307L, 
    14877194L, 11216973L, 17906140L, 11152617L, 15247289L, 9919111L, 
    30596442L, 31624492L, 6494032L, 29419861L, 22205115L, 11612651L, 
    12506364L, 3977433L, 9251065L, 52886830L, 30459500L, 10314486L, 
    62828525L, 16266340L, 11414242L, 11315183L, 14865891L, 9219453L, 
    19531171L, 12390920L, 554893L, 6871604L, 56267484L, 13478614L, 
    59179677L, 56843397L, 33077108L, 29693238L, 5709800L, 10984800L, 
    62188950L, 3424377L, 55865206L, 33250188L, 11883725L, 61208251L, 
    14620505L, 33824988L, 9605466L, 10086150L, 43150201L, 2434175L, 
    13636535L, 9149052L, 12859396L, 15342563L, 299795L, 6557079L, 
    29079786L, 35154155L, 5720813L, 3213738L, 54942898L, 2011266L, 
    7788028L, 17631115L, 26168243L, 55355445L, 12363848L, 32406026L, 
    5181923L, 3045645L, 55879245L, 27803689L, 12997556L, 9993556L, 
    24094397L, 61278488L, 16146261L, 30860019L, 4851695L, 16059845L, 
    9734641L, 9682186L, 1434076L, 787507L, 33627937L, 35299118L, 
    31196723L, 23152630L, 18430364L, 16404624L, 21956375L, 31145208L, 
    29812678L, 28715201L, 31587264L, 18888020L, 14560381L, 28866133L, 
    11402958L, 28100484L, 21889495L, 33006788L, 32671209L, 22527130L, 
    36680524L, 30021920L, 35047621L, 33187732L, 30610149L, 34511947L, 
    21200181L, 31763855L, 16891242L, 28242299L, 31096620L, 35093501L, 
    28600363L, 14257733L, 32070016L, 10522891L, 785L, 17111781L, 
    25138826L, 21459015L, 28940910L, 21906624L)), row.names = c(NA, 
-219L), class = c("data.table", "data.frame"), .Names = c("ext_install_date", 
"sum"), .internal.selfref = <pointer: 0x0000000001290788>)

The ggvis code:

library(ggvis)

for_plot %>% ggvis(x = ~ext_install_date, y = ~sum, stroke := "red") %>% layer_lines(stroke=2) %>%
      add_tooltip( function(data){(data$ext_install_date)}, "hover") #showing only numeric values

thanks or any help on that!


Solution

  • Unfortunately the tooltip in ggvis doesn't handle the values underpinning layer_lines as separate data points. Just as in your example, it only displays the first value. The second problem is the way the tooltip displays date objects.

    We can hack our way around both problems, with the same approach as outlined here. Please note that the tooltip will still erroneously show the first date in between data points.

    for_plot %>% ggvis(x = ~ext_install_date, y = ~sum) %>% 
     layer_points(opacity:=0) %>%
     add_tooltip( function(data){(as.Date(data$ext_install_date/86400000,    
     origin='1970-01-01'))}) %>%
     layer_lines()
    

    I believe the googleVis package doesn't suffer from the same issues, so you might want to consider using it for similar graphs in the future.