Search code examples
rr-highcharter

Highcharter bar chart cut off x axis label


I'm doing some plot with highcharter, but have some issues with label when ploting stacked barchart whan I have only one category. When I have more than one category it works well, how can I fix that ? Thanks

df <- structure(list(nom_part="BOB", id_part="565626235", fact_cada_annee="2018", ok=1), 
                row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame"))

highchart() %>% 
  hc_chart(type = "column") %>% 
  hc_plotOptions(column = list(stacking = "normal")) %>%
  hc_xAxis(categories = df$fact_cada_annee) %>%
  # hc_add_series(name="Autres",
  #               data = df$autres,
  #               stack = "Assets") %>%
  # hc_add_series(name="Ko",
  #               data = df$ko,
  #               stack = "Assets") %>% 
  hc_add_series(name="Ok",
                data = df$ok,
                stack = "Assets") %>% 
  hc_title(text = "Evolution note cadastre par année")

Solution

  • You could try the simpler hchart way. For my example, I use the data from highcharter page:

    data(mpg,package='ggplot2')
    mpgman1 <-mpg %>% count(class, year)
    mpgman2 <-mpg %>% count(class, year) %>% filter(class == '2seater')
    mpgman3 <-mpg %>% count(class, year) %>% filter(class =='2seater',year == 1999)
    

    Now mpgman1, mpgman2, mpgman3 are used to be plotted as follows:

    hchart(mpgman1, "column", hcaes(x = class, y = n, group = year))%>%
     hc_plotOptions(column = list(stacking = "normal"))
    

    that works also for the case when one group exists:

    hchart(mpgman2, "column", hcaes(x = class, y = n, group = year))%>%
     hc_plotOptions(column = list(stacking = "normal"))
    

    or even when one level exists:

     hchart(mpgman3, "column", hcaes(x = class, y = n, group = year))%>%
     hc_plotOptions(column = list(stacking = "normal"))