highcharts R highcharter列图的x轴顺序

8gsdolmq  于 2022-11-10  发布在  Highcharts
关注(0)|答案(1)|浏览(195)

具有以下数据框:

dta <- structure(list(sociodemographic_var = structure(c(3L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 26L, 18L, 20L, 21L, 26L, 13L, 16L, 21L, 22L, 26L, 26L, 
9L, 13L, 17L, 18L, 20L, 21L, 23L, 26L, 20L, 26L), levels = c("1st grade", 
 "2nd grade", "3rd grade", "4th grade", "5th grade", "6th grade", 
 "7th grade", "8th grade", "9th grade", "10th grade", "11th grade", 
 "12th grade, no diploma", "High school graduate", "GED or equivalent", 
 "Some college, no degree", "Less than 1 year of college credit/post-secondary education (or less than 10 classes)", 
 "One year or more of college credit, no degree", "Associate degree: Occupational, Technical, or Vocational", 
 "Associate degree: Academic Program", "Bachelor's degree (ex. BA, AB, BS, BBS)", 
 "Master's degree (ex. MA, MS, MEng, MEd, MBA)", "Professional School degree (ex. MD, DDS, DVN, JD)", 
 "Doctoral degree (ex. PhD, EdD)", "Refused to answer", "Don't Know", 
 "unknown"), class = "factor"), event = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 7L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 11L, 11L), levels = c("Baseline", "0.5 Year", "1 Year", 
"1.5 Year", "2 Year", "2.5 Year", "3 Year", "3.5 Year", "4 Year", 
"4.5 Year", "5 Year", "5.5 Year", "6 Year", "Screener"), class = "factor"), 
 visit_type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), levels = c("on-site", "hybrid", "remote", "unknown"), class = "factor"), 
 n = c(2L, 13L, 5L, 9L, 15L, 18L, 26L, 25L, 192L, 27L, 485L, 
 224L, 183L, 1011L, 666L, 55L, 78L, 3L, 9L, 1L, 1L, 2L, 208L, 
 1L, 1L, 1L, 1L, 126L, 28L, 1L, 1L, 2L, 2L, 3L, 4L, 1L, 543L, 
 1L, 300L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
 -39L))

我会假设,生成一个highcharter条形图:

library(highcharter)  # v0.9.4

dta |> 
  hchart(type = "column", hcaes(x = "event", y = "n", group = "sociodemographic_var")) |> 
  hc_yAxis(title = list(text = "%"), max = 115, endOnTick = FALSE, stackLabels = list(enabled = TRUE)) |> 
  hc_xAxis(title = "") |> 
  hc_plotOptions(series = list(stacking = "percent"))

x轴将按levels(dta$event)排序:

levels(dta$event)
[1] "Baseline" "0.5 Year" "1 Year"   "1.5 Year" "2 Year"   "2.5 Year" "3 Year"   "3.5 Year" "4 Year"   "4.5 Year" "5 Year"   "5.5 Year"
[13] "6 Year"   "Screener"

但排序不同,既不按字母顺序,也不基于值的总数:

我有兴趣了解为什么会这样,以及如何正确设置顺序。

wgxvkvu9

wgxvkvu91#

您可以将categories添加到您的hc_xAxis,以生成如下顺序:

library(highcharter) 

dta |> 
  hchart(type = "column", hcaes(x = "event", y = "n", group = "sociodemographic_var")) |> 
  hc_yAxis(title = list(text = "%"), max = 115, endOnTick = FALSE, stackLabels = list(enabled = TRUE)) |> 
  hc_xAxis(title = "", categories = levels(dta$event)) |> 
  hc_plotOptions(series = list(stacking = "percent"))

输出量:

相关问题