How to produce scatterplot with a factor as y in highcharter?

霸气de小男生 提交于 2019-12-01 08:13:32

问题


Problem:

I would like to produce a scatter plot with highcharter::hchart, where y is a factor, and x is a date.

Apparently, highcharter::hchart "scatter" does not accept factor variables as y.

Is there any workaround? Or is "scatter" just the wrong charttype?

(Comment: I know ggplotly would be a good alternative, but I actually need a highcharter solution)


Example:

Let's suppose I want to produce a timeline of publications by type. I want a scatterplot with d$type (=y-axis) and d$date (=x-axis) and the highcharter tooltip should show me d$title, d$type and d$date (properly formatted).

library(stringi)
library(tidyverse)
library(highcharter)

### create example data
d <- data.frame(date = sample(seq(as.Date("2001/1/1"),
                                  as.Date("2003/1/1"),
                                  by = "day"),
                              30), # Date of publication
                title = stringi::stri_rand_strings(30, 5), # Title of publication
                type = rep(c("book","article","tweet"),
                           length.out=30)) # Publication type
glimpse(d)
#>Observations: 30
#>Variables: 3
#>$ date  <date> 2001-02-21, 2001-12-31, 2002-09-02, 2002-12-11, 2001-...
#>$ title <fct> NvHuI, 81HoS, TsyWs, KbTT2, I2p4f, ASasv, HuclA, cmihb...
#>$ type  <fct> book, article, tweet, book, article, tweet, book, arti...


### ggplot2: static plot
ggplot(d, aes(x=date,
              y=type)) +
  geom_point(aes(colour=type), 
             alpha=0.5,
             size = 3) + 
  scale_x_date(date_labels = "%m / %Y") +
  theme_light() +
  theme(panel.grid.minor.x = element_blank())

ggplot2::geom_points does a nice job here.

However, I want the chart to be interactive (tooltip showing the title and so on...) So I'll give it a try with highcharter::hchart:

### highcharter: interactive plot
# A.) NOT WORKING, because y is a factor
hchart(d,
       "scatter",
       hcaes(x = date,
             y = type,
             group = type))   

Apparently, highcharter::hchart "scatter" doesn't accept a factor as y.

The only way I get this working is transforming d$type to numeric...but then the xAxisLabels and the tooltip are wrong...

# B.) WORKING, because y is numeric
hchart((d %>% 
          mutate(typenum = as.numeric(type))), 
       "scatter",
       hcaes(x = date,
             y = typenum,
             group = typenum))   


回答1:


An alternative would be:

lvls <- d %>% pull(type) %>% levels()

d %>% 
  mutate(typenum = as.numeric(type) - 1) %>% 
  hchart("scatter", hcaes(x = date, y = typenum, group = type)) %>% 
  hc_yAxis(categories = lvls)

Note the as.numeric(type) - 1, this is beacause Javascript and then highcharts is 0-index. So when we add the name of categories, highcharts will start with 0.



来源:https://stackoverflow.com/questions/49034106/how-to-produce-scatterplot-with-a-factor-as-y-in-highcharter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!