【问题标题】:Why highcharter give random plot (date x-axis)?为什么 highcharter 给出随机图(日期 x 轴)?
【发布时间】:2020-07-07 14:42:20
【问题描述】:

在这里,我有 2 个相似的数据框,但输出图与 highcharter 不同。

变量的类型相似……是随机动作吗?

是否可以强制 highcharter 给出类似于第二个 (df2) 的情节?

感谢您的帮助

EDITt 1:我尝试将年份日期转换为整数(和字符),但行为最糟糕..

编辑 2:我找到了解决方案,但我认为这是一个错误 ...

数据和脚本:

library(data.table)
library(highcharter)

df1 = structure(list(
  stat_motif = c("autre", "controle_cadastre", "doc_apres_travaux", 
                 "doc_apres_travaux", "doc_avant_travaux", "inconnu", "inconnu"),
  nb_stat_annee = c(1L, 10L, 4L, 1L, 1L, 601L, 573L), 
  annee_modif_stat = structure(c(18262, 18262, 18262, 17897, 18262, 18262, 17897), class = "Date"), 
  col_motif = c("#87CEEB", "#7CFC00", "#FAA43A", "#FAA43A", "#FF0000", "#C0C0C0", "#C0C0C0")), 
  class = "data.frame", row.names = c(NA, -7L), .Names = c("stat_motif", "nb_stat_annee", "annee_modif_stat", "col_motif"))

df2 = structure(list(
  stat = c("abandonne", "admissible", "admissible", 
                            "admissible", "finalise_pro", "refuse", "refuse"), 
  nb_stat = c(3L, 122L, 1L, 5L, 1L, 7L, 1L),
  annee_enreg_stat = structure(c(17532, 17897, 17532, 18262, 17897, 17897, 17532), class = "Date"),
  col = c("#FAA43A", "#7CFC00", "#7CFC00", "#7CFC00", "#264D00", "#FF0000", "#FF0000")), row.names = c(NA, -7L), class = "data.frame", .Names = c("stat", "nb_stat", "annee_enreg_stat", "col"))





setDT(df1)
setDT(df2)

# X-axis problem with this code
df1 = unique(df1[order(stat_motif)])
# EDIT 2 with 'solution'
# df1 = unique(df1[order(stat_motif, annee_modif_stat)])
# Or
# df1 = df1[stat_motif != "inconnu"]

col_vec = unique(df1[, col_motif])

hchart(
  as.data.frame(df1),
  "column", 
  hcaes(x = annee_modif_stat, y = nb_stat_annee, group = stat_motif)
) %>%
  hc_plotOptions(column = list(stacking = "normal"), type = "datetime") %>% 
  hc_xAxis(
    title = list(text = 'Date')
  ) %>% 
  hc_yAxis(
    title = list(text = "Nb")
  ) %>% 
  hc_add_theme(hc_theme_gridlight()) %>% 
  hc_colors(colors = col_vec)


df2 = unique(df2[order(stat), list(stat, nb_stat, annee_enreg_stat, col)])

col_vec = unique(df2[, col])

hchart(
  as.data.frame(df2),
  "column", 
  hcaes(x = annee_enreg_stat, y = nb_stat, group = stat)
) %>% 
  hc_plotOptions(column = list(stacking = "normal"), type = "datetime") %>% 
  hc_xAxis(
    title = list(text = 'Date')
  ) %>% 
  hc_yAxis(
    title = list(text = "Nb")
  ) %>% 
  hc_add_theme(hc_theme_gridlight()) %>% 
  hc_colors(colors = col_vec)

【问题讨论】:

  • 如果您找到了解决方案,那么您最好能自行回答您的问题,因为其他人可能会遇到类似的问题
  • 当然,我会发布一个“解决方案”,但我希望有人能给出一个可靠的解决方案并给出一些解释

标签: r data.table r-highcharter


【解决方案1】:

要查看 Highcharter 结果,将其分配给对象hc1 <- hchart(... 很有用。我通常会查看hc1$x$hc_opts$plotOptionshc1$x$hc_opts$series

在上面的示例中,Highcharter 生成的对象除了数据之外都相似(与 all.equal(hc1, hc2).IMO 相比,不同的 xAxis 是来自 Highcharts 库的结果。

要强制 xAxis 使用年度列,我建议将日期转换为年份字符串 (df1$annee_modif_stat <- substr(df1$annee_modif_stat, 1,4)) 并使用 type = "category" 生成图表。

Highcharts API 文档有助于查找可用选项,通过查看 Highcharter 对象,您可以在 JSON 转换之前查看对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    相关资源
    最近更新 更多