【发布时间】: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