R Highcharter:格式化双y轴图表

时间:2018-06-27 19:26:52

标签: r highcharts r-highcharter

我有一个使用主要和次要y轴的柱形图和折线图。我想知道如何分别格式化每个轴。

我希望能够做两件事:

  • 已将主轴最大值设置为100
  • 能够将colorByPoint用于列系列。当前,当我使用该参数时,行系列不会出现。

下面是我的数据集:

> dput(SiteOTD)
structure(list(SBU = c("LS", "LS", "LS", "INT", "LS", "APS", 
"LS", "CS", "INT", "CS", "SIS", "CS", "APS", "ES", "ASG", "CS", 
"INT", "ASG", "ES", "ES", "APS", "CS", "SIS", "CS", "CS", "SIS", 
"APS", "CS", "CS", "ASG", "ASG", "CS", "ES", "CS", "SIS", "ASG", 
"APS", "ASG", "CS", "SIS", "CS", "SIS", "APS", "CS", "ES", "LS"
), Region = c("IATA III", "IATA I", "IATA I", "IATA I", "IATA III", 
"IATA I", "IATA II", "IATA III", "IATA I", "IATA I", "IATA I", 
"IATA II", "IATA II", "IATA I", "IATA II", "IATA I", "IATA I", 
"IATA I", "IATA I", "IATA II", "IATA II", "IATA I", "IATA II", 
"IATA I", "IATA II", "IATA I", "IATA II", "IATA I", "IATA III", 
"IATA II", "IATA II", "IATA III", "IATA I", "IATA I", "IATA II", 
"IATA I", "IATA II", "IATA III", "IATA III", "IATA I", "IATA II", 
"IATA I", "IATA II", "IATA II", "IATA II", "IATA I"), Plant.Name = c("Witherhill Park", 
"Louisville", "Los Angeles", "Jamestown", "Hong Kong", "Oakville", 
"London", "Xiamen", "Phoenix", "Phoenix", "Wilson", "Dubai", 
"Wolverhampton", "Rome", "Prestwick", "Miramar", "Colorado", 
"Chula Vista", "Windsor Locks", "Marston", "Torino", "Puerto Rico", 
"LHotellier", "Monroe", "Maastricht", "Burnsville", "Vernon", 
"Rockford", "Singapore", "Turkey", "Toulouse", "FAST", "Peabody", 
"NURS", "Plymouth", "Foley", "Brugherio.Luserna", "Singapore", 
"Malaysia", "Vergennes", "Paris", "Brea", "St Ouen", "Dijon", 
"Marston Green", "Wichita"), OTD = c(0.988165680473373, 0.918974782968169, 
0.907429245283019, 0.84375, 0.822389666307858, 0.782608695652174, 
0.776832460732984, 0.765151515151515, 0.734375, 0.718238993710692, 
0.707799767171129, 0.690721649484536, 0.687732342007435, 0.675, 
0.663829787234043, 0.641509433962264, 0.636363636363636, 0.629955947136564, 
0.60202260559191, 0.601351351351351, 0.6, 0.59, 0.586206896551724, 
0.566975568660489, 0.558333333333333, 0.548062015503876, 0.534743202416918, 
0.521978021978022, 0.51063829787234, 0.5, 0.496240601503759, 
0.488888888888889, 0.485294117647059, 0.484848484848485, 0.470588235294118, 
0.448818897637795, 0.440677966101695, 0.436619718309859, 0.435672514619883, 
0.397752808988764, 0.396648044692737, 0.367647058823529, 0.322147651006711, 
0.304568527918782, 0.232876712328767, 0.07981220657277), Shipments = c(169, 
2419, 1696, 32, 929, 69, 1528, 132, 128, 795, 859, 97, 269, 40, 
236, 530, 22, 227, 1681, 149, 276, 100, 29, 1187, 600, 1290, 
331, 182, 611, 6, 133, 90, 272, 338, 51, 128, 118, 142, 344, 
445, 179, 68, 149, 197, 292, 213)), row.names = c(NA, -46L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), vars = c("SBU", "Region"), drop = TRUE, .Names = c("SBU", 
"Region", "Plant.Name", "OTD", "Shipments"), indices = list(5L, 
    c(12L, 20L, 26L, 36L, 42L), c(17L, 35L), c(14L, 29L, 30L), 
    37L, c(9L, 15L, 21L, 23L, 27L, 33L), c(11L, 24L, 40L, 43L
    ), c(7L, 28L, 31L, 38L), c(13L, 18L, 32L), c(19L, 44L), c(3L, 
    8L, 16L), c(1L, 2L, 45L), 6L, c(0L, 4L), c(10L, 25L, 39L, 
    41L), c(22L, 34L)), group_sizes = c(1L, 5L, 2L, 3L, 1L, 6L, 
4L, 4L, 3L, 2L, 3L, 3L, 1L, 2L, 4L, 2L), biggest_group_size = 6L, labels = structure(list(
    SBU = c("APS", "APS", "ASG", "ASG", "ASG", "CS", "CS", "CS", 
    "ES", "ES", "INT", "LS", "LS", "LS", "SIS", "SIS"), Region = c("IATA I", 
    "IATA II", "IATA I", "IATA II", "IATA III", "IATA I", "IATA II", 
    "IATA III", "IATA I", "IATA II", "IATA I", "IATA I", "IATA II", 
    "IATA III", "IATA I", "IATA II")), row.names = c(NA, -16L
), class = "data.frame", vars = c("SBU", "Region"), drop = TRUE, .Names = c("SBU", 
"Region")))

下面是我的代码:

input1 <- "APS"
input2 <- NULL
input3 <- NULL

if (is.null(input1) & is.null(input2) & is.null(input3)) {SiteOTD$Colors <- "#003395"
} else if (!is.null(input1) & is.null(input2) & is.null(input3)) {SiteOTD$Colors <- ifelse(SiteOTD$SBU == input1,"#000000","#003395")}

highchart() %>%
  hc_yAxis_multiples(list(title = list(text = "%, OTD"),lineWidth = 0), list(title = list(text = "Qty, Volume"), opposite = TRUE)) %>% 
  hc_add_series(SiteOTD, "column", hcaes(x = Plant.Name, y = round(OTD,2)*100), name = "On Time Delivery", yAxis = 0, dataLabels = list(enabled = TRUE, fontSize = "0.5vh")) %>%
  hc_add_series(SiteOTD, "line", hcaes(x = Plant.Name, y = Shipments), name = "Volume", marker = "diamond", color = "#FF7900", yAxis = 1) %>%
  hc_xAxis(title = "Sites", categories = paste0(SiteOTD$Plant.Name,"-",SiteOTD$SBU), labels = list(step = 1, rotation = -60, style = list(fontSize = "1.0vh"))) %>%
  hc_tooltip(crosshairs = TRUE, shared = TRUE, headerFormat = "<b>{point.x}</b><br>") %>%
  hc_legend(enabled = FALSE) %>%
  hc_plotOptions(series = list(colorByPoint = TRUE, type = "column", colors = SiteOTD$Colors))

如果删除hc_plotOptions,则会显示行系列。如果保留,colorByPoint可以使用,但是行系列消失。

1 个答案:

答案 0 :(得分:0)

想通了!我可以将max放在hc_yAxis_multipleshc_plotOptions中,而不是使用series,而是使用column

highchart() %>%
            hc_yAxis_multiples(list(title = list(text = "%, OTD"),lineWidth = 0, labels = list(format = "{value}%"), max = 100), list(title = list(text = "Qty, Volume"), opposite = TRUE)) %>%
            hc_xAxis(title = "Sites", categories = paste0(SiteOTD$Plant.Name,"-",SiteOTD$SBU), labels = list(step = 1, rotation = -60, style = list(fontSize = "1.0vh"))) %>%
            hc_add_series(SiteOTD, "column", hcaes(x = Plant.Name, y = round(OTD,2)*100), name = "On Time Delivery", yAxis = 0) %>%
            hc_add_series(SiteOTD, "line", hcaes(x = Plant.Name, y = Shipments), name = "Volume", marker = "diamond", color = "#FF7900", yAxis = 1) %>%
            hc_tooltip(crosshairs = TRUE, headerFormat = "<b>{point.x}</b><br>") %>%
            hc_legend(enabled = FALSE) %>%
            hc_plotOptions(column = list(colorByPoint = TRUE, colors = SiteOTD$Colors))

这删除了shared = TRUEhc_tooltip的功能,但这有点烦人。