使用R中的highcharter为highchart中的误差线图设置双轴标签

时间:2019-04-30 20:25:32

标签: r highcharts errorbar r-highcharter

我希望使用R中的highcharter包在高图中创建一个由散点图和水平误差线组成的图。我希望​​在图上有两个y轴标签,左侧将显示每个错误栏的名称和右侧将是错误栏的值。

基于herehere,我知道可以为Y轴创建双轴标签。但是,为了使误差线水平,我必须反转/翻转轴(高图中的误差线是垂直的)。因此,我实际上需要具有两个 X轴标签,然后翻转坐标。

我包括了到目前为止我创建的代码,这些代码可以创建散点图和水平误差线,并且在左侧具有名称。

谢谢!

# load pacakge ---------------
if(!require(tidyverse)) {install.packages("tidyverse"); library(tidyverse)}
if(!require(magrittr)) {install.packages("magrittr"); library(magrittr)}
if(!require(highcharter)) {install.packages("highcharter"); library(highcharter)}

# sample data
dd <- read.table(textConnection("Abbreviation   Adjustment  Effect  x_dummy lwr_final   med_final   upr_final
PBO A   AA  0   7.37    12.7    18.82
PBO A   BB  2   1.77    9.91    40.32
PBO B   AA  4   7.37    12.7    18.82
PBO B   BB  6   1.77    9.91    40.32
ABP A   AA  1   3.07    4.63    6.89
ABP A   BB  3   0.26    1.72    7.35
ABP B   AA  5   3.07    4.63    6.89
ABP B   BB  7   0.26    1.72    7.35
"), header = T)

# create group column
dd %<>%
  mutate(grp1 = paste0(Adjustment, " (", Effect, ")"))

# category label for plot from 
cat_lab <- dd %>%
  dplyr::group_by(name = Abbreviation) %>%
  dplyr::do(categories = array(.$grp1)) %>%
  highcharter::list_parse()

# creating plot
pp <- highcharter::hchart(dd, "errorbar",
                         highcharter::hcaes(x = x_dummy, low = lwr_final, high = upr_final),
                         color = "black", whiskerLength = 5) %>%
  highcharter::hc_add_series(dd, "scatter",
                             highcharter::hcaes(x = x_dummy, y = med_final),
                             marker = list(radius = 2),
                             color = "red") %>%
  highcharter::hc_xAxis(categories = cat_lab) %>%
  # flip coordinates
  highcharter::hc_chart(inverted = TRUE)
pp

0 个答案:

没有答案