【问题标题】:Creating an index bar chart in Highcharter在 Highcharter 中创建索引条形图
【发布时间】:2023-03-08 15:42:01
【问题描述】:

我正在尝试创建一个条形图,显示在 R 的 Highcharter 包中索引到美国价值 (100) 的生活成本。所以我希望条形图从 100 开始,并且延伸到右侧或左侧100. 我有基本的条形图,但我不知道如何将图表的中心线移动到 100。我试图让它看起来像这样:http://drawingwithnumbers.artisart.org/moving-the-center-line-of-a-bar-chart-with-a-gantt-chart/

这是我正在处理的数据:

States <- c('Tennessee','Michigan','Indiana','Ohio','Kentucky','West Virginia','North Carolina','Virginia','Pennsylvania','Delaware','New Jersey','Maryland','New York')
Cost_of_Living <- c(88.7,88.9,90,90.8,90.9,91.1,94.9,100.7,101.7,108.1,125.1,129.7,139.1)
costliving <- data.frame(States, Cost_of_Living)

这是条形图的代码,只是基本条形图

costliving_graph <- highchart() %>% 
  hc_title(text = "Cost of Living by State (2020)") %>%
  hc_add_series (costliving, "bar", hcaes(x = States, y = Cost_of_Living)) %>%
  hc_xAxis(categories = costliving$States) %>%
  hc_yAxis(title = list(text = "Indexed to the U.S. (U.S. Value = 100)"))%>%
  hc_plotOptions (bar = list(colorByPoint = TRUE)) %>%
  hc_legend (enabled = FALSE)%>%
  hc_tooltip(pointFormat = "MSA Regional Price Parities: {point.y}", headerFormat ="")
costliving_graph

任何帮助都会很棒!提前致谢

【问题讨论】:

    标签: r r-highcharter


    【解决方案1】:

    在 highcharter 网站上查看示例后,我将图表类型更改为 columnrange,并在数据框中添加了新的变量 lowhigh。试试这个:

    library(highcharter)
    library(dplyr)
    
    States <- c('Tennessee','Michigan','Indiana','Ohio','Kentucky','West Virginia','North Carolina','Virginia','Pennsylvania','Delaware','New Jersey','Maryland','New York')
    Cost_of_Living <- c(88.7,88.9,90,90.8,90.9,91.1,94.9,100.7,101.7,108.1,125.1,129.7,139.1)
    costliving <- data.frame(States, Cost_of_Living) %>% 
      mutate(low = pmin(Cost_of_Living, 100),
             high = pmax(Cost_of_Living, 100))
    
    costliving_graph <- highchart() %>% 
      hc_title(text = "Cost of Living by State (2020)") %>%
      hc_add_series(costliving, "columnrange", hcaes(x = States, y = Cost_of_Living, low = low, high = high)) %>%
      hc_xAxis(categories = costliving$States) %>%
      hc_yAxis(title = list(text = "Indexed to the U.S. (U.S. Value = 100)"))%>%
      hc_plotOptions (bar = list(colorByPoint = TRUE)) %>%
      hc_legend (enabled = FALSE)%>%
      hc_tooltip(pointFormat = "MSA Regional Price Parities: {point.y}", headerFormat ="")
    costliving_graph
    

    【讨论】:

    • 效果很好!谢谢!我将 hc_tooltip 部分编辑为hc_tooltip(pointFormat = "MSA Regional Price Parities: {point.Cost_of_Living}", headerFormat =""),以便工具提示以这种方式显示索引。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-06-28
    • 2021-12-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多