繁体   English   中英

如何使用 highcharter 在 r 中创建工具提示图表?

[英]How to create a tooltip chart in r using highcharter?

我正在使用highcharter库并参考下面的链接在气泡图中创建交互式工具提示图表

https://jkunst.com/blog/posts/2019-02-04-using-tooltips-in-unexpected-ways/

Plot 图像:

在此处输入图像描述

使用链接中显示的gapminder数据,我能够重现相同的数据,但是当我使用其他数据时,工具提示图表不会出现。

我的其他数据的代码:

library(tidyverse)
library(highcharter)

数据

grouped_cases_df <- read.csv("https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/grouped_cases.csv")
tt_base <- grouped_cases_df %>% 
  arrange(desc(Date)) %>% 
  distinct(Country.Region, .keep_all = TRUE)

tt_base 
tt_inner <- grouped_cases_df %>% 
  select(Country.Region, Date, Daily_cases) %>% 
  nest(-Country.Region) %>% 
  mutate(
    data = map(data, mutate_mapping, hcaes(x = Date, y = Daily_cases), drop = TRUE),
    data = map(data, list_parse)
  ) %>% 
  rename(tt_nestdata = data)

tt_inner
tt_daily <- left_join(tt_base, tt_inner, by = "Country.Region")

tt_daily
hchart(
  tt_daily,
  "point",
  hcaes(x = Active, y = Confirmed, name = Country.Region,
        size = Daily_cases, group = continent, name = Country.Region) 
) %>% 
  
  hc_yAxis(type = "logarithmic") %>%
  
  hc_tooltip(
  useHTML = TRUE,
  headerFormat = "<b>{point.key}</b>",
  pointFormatter = tooltip_chart(accesor = "tt_nestdata")
  ) %>% 
  
  hc_title(text = "Active Vs Confirmed Cases as of latest Date") %>% 
  hc_subtitle(text = "Size of bubble based on Deaths <br> (ttchart: population growth)")

问题:获取每个国家/地区的空白工具提示图表。

在此处输入图像描述

我还尝试将Country.Region更改为as.factor()但没有帮助。 我不确定这有什么问题。

需要进行两个更改:

  1. 工具提示数据需要为 highcharter 做好准备。 因此,您需要将日期列从文本转换为日期,然后转换为 highcharts 可以解释为日期的数值:
mutate(Date = highcharter::datetime_to_timestamp(lubridate::ymd(Date)))
  1. 然后,在tooltip_chart function 中的hc_opts参数中,您需要指定 x 轴将值视为日期。
pointFormatter = tooltip_chart(accesor = "tt_nestdata", hc_opts = list(xAxis = list(type = "datetime")))

然后:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM