R Highcharter:在向下钻取中绘制其他图表

问题描述 投票:0回答:1

我想为具有不同绘图选项的向下钻取绘制不同的图表类型。例如:“主图表”将是按组的折线图,而“向下钻取图”将是一个柱形图。

Highcharts(Highcharts, Can you change the chart type for drilldowns?)中提供了一个解决方案,但我无法将其转换为R。

PFB代码。

library("dplyr")
library("purrr")

df <- data_frame(
  car_brand = c("Hyundai","Hyundai","Hyundai", "Benz","Benz","Benz", "Tesla","Tesla","Tesla"),
  units_sold = c(10,15,20,11,8,13,6,5,7),
  date = c("2019-01-01", "2019-02-01","2019-03-01","2019-01-01","2019-02-01","2019-03-01","2019-01-01","2019-02-01","2019-03-01")
)

df$units_sold <- as.numeric(df$units_sold)
df$date <- as.Date(df$date)
df$drilldown <- paste(df$car_brand, ",", df$date)
carBrands<- df %>%
  select(date, car_brand)

getCarDetails<- function(brands){

  carList <- list()
  listofdfs <- list() #Create a list in which you intend to save your df's.

  for(i in 1:nrow(brands)){ #Loop through the numbers of ID's instead of the ID's

    #You are going to use games[i] instead of i to get the ID
    BrandCarData <- data_frame(
      car = c("H1","H2","H3","H4","H5"),
      units = c(1,2,3,4,5)
    )
    BrandCarData$units <- as.numeric(BrandCarData$units)
    dsCar <- list_parse2(BrandCarData)
    listofdfs[[i]] <- dsCar
    carList[[i]] <- list (name = brands[[2]][i],
                          type = "column",
                          id = paste(brands[[2]][i], ",", brands[[1]][i]),
                          data = listofdfs[[i]])
  }

  return(carList) #Return the list of dataframes.
}

listCar <- getCarDetails(brands = carBrands)

hc <- hchart(df,"line", hcaes(x=date, y =
                                units_sold, group
                                       = car_brand )) %>%
  hc_xAxis(categories = dfDates$date, title = list(text = "<b>Date</b>"), type = "datetime") %>%
  hc_plotOptions(column = list(dataLabels = list(enabled = FALSE), enableMouseTracking = TRUE))%>%
  hc_tooltip(borderWidth = 1.5,
             pointFormat = paste('<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>')) %>%
  hc_legend(enabled = TRUE) %>%
  hc_title(text = "Brand Units Sold Monthy Trend",
           style = list(fontSize = "12px", fontWeight = "bold")) %>%
  hc_yAxis(title = list(text = "<b>Units <br>(In Thousands)</br></b>"))
hc

hc1 <- hc %>%
  hc_drilldown(allowPointDrilldown = TRUE,
               series = listCar)
hc1

Brand-wise Units Sold Monthly Trend

Units Sold for Each Car Model

从附图中可以看出,第二张图表具有与第一张图表相同的图表属性。我想更改第二个图表的标题,将x轴类型从DateTime更改为类别,并更改轴标签。

任何建议。

r highcharts r-highcharter
1个回答
0
投票

类似问题:Drilldown to multiple series from different groups - Highcharter (in R)

您也可以在R(Highcharter)中找到类似的Highcharts细分示例:https://stackoverflow.com/users/8618934/raf18seb?tab=answers

© www.soinside.com 2019 - 2024. All rights reserved.