本文介绍了如何在highcharter中以y为因子生成散点图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:



我想用 highcharter :: hchart ,其中 y 是因子,x是日期 $ b

显然, highcharter :: hchart scatter 不接受因素变量作为y。



是否有任何解决方法?
或者scatter只是错误的图表类型?



(评论:我知道 ggplotly 会是一个不错的选择,但实际上我需要一个 highcharter 解决方案)






举例:



假设我想根据类型生成发布时间表。
我想要一个带有 d $ type (= y轴)和 d $ date 的散点图(= x轴)和 highcharter 工具提示应该显示 d $ title , d $ type 和 d $ date (格式正确)。

 <$ c $ b $ library $(
$ library

$ lt; - data.frame(date =样本(seq(as.Date(2001/1/1),
as.Date(2003/1/1),
by =day),
30 ),#发布日期
title = stringi :: stri_rand_strings(30,5),#发布的标题
type = rep(c(book,article,tweet),
$ b#>观察值:30
#>变量:3
#> $日期<日期> 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>书籍,文章,推文,书籍,文章,推文,书籍,艺术...


### ggplot2:静态图
ggplot(d,aes(x = date ,
y = type))+
geom_point(aes(color = 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 在这里干得不错。然而,我希望图表是交互式的(工具提示显示标题等等)。所以我会试着用 highcharter :: hchart :

  ### highcharter:interactive plot 
#A )不工作,因为y是一个因素
hchart(d,
scatter,
hcaes(x = date,
y = type,
group = type) ))

显然, highcharter :: hchartscatter不接受因子为 y 。



我得到这个工作的唯一方法是将 d $ type 转换为numeric ...,然后将 xAxisLabels 和工具提示是错误的......

 #B.)WORKING,因为y是数字
hchart((d %>%
mutate(typenum = as.numeric(type))),
scatter,
hcaes(x = date,
y = typenum,
group = typenum))


解决方案

另一种方法是:

  lvls<  -  d%>%拉(类型)%>%levels()

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


请注意 as.numeric(type) - 1 ,这是因为Javascript和highcharts是0-索引。所以当我们添加类别的名称时,highcharts将从0开始。


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))   
解决方案

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.

这篇关于如何在highcharter中以y为因子生成散点图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 22:06