Search code examples
rhighchartsr-highcharter

How to remove white spaces on highcharts chart in R


This is my data:

 df_cards <- structure(list(x = c("Jan - 2021", "Feb - 2021", "Mar - 2021", 
"Apr - 2021", "May - 2021"), y = c(22.7851010020822, 24.7339859977365, 
27.2432568809018, 29.1082223854028, 29.049789654091), colors = c("#0F60D2", 
"#0F60D2", "#EF2D3E", "#EF2D3E", "#EF2D3E")), class = "data.frame", row.names = c("color1", 
"color2", "color3", "color4", "color5"))

This is the chart code:

hchart(
  df_cards,
  "area",
  hcaes(x, y, color = colors),

  name = "Revenue",
  color = sample_2_colors,
  showInLegend = TRUE
)

This is the plot: enter image description here

How can I remove the white spaces that I pointed on the chart on the left and on the right?


Solution

  • To fix your issue convert your dates to proper dates and set your desired format for the axis labels via hc_xAxis:

    library(highcharter)
    
    df_cards$x <- as.Date(paste("2021", 1:5, 1, sep = "-"))
    
    hchart(
      df_cards,
      "area",
      hcaes(x, y, color = colors),
      name = "Revenue",
      showInLegend = TRUE
    ) |>
      hc_xAxis(labels = list(format = "{value:%b - %Y}"))
    

    enter image description here