Search code examples
rr-highcharter

Highcharter-R: x-axis values not showing up, wrong text on y-axis


I am trying to create a mixed chart with highcharter R, see below the full code. I can't get rid of the following problems:

  1. how to delete the text "Values" on both y-axes? I'd like only my text to show up ("xxx" and "yyy").
  2. why are the x-axis values not showing up (2019, 2020a, 2020b) and instead below the columns "1", 2" and "3" appear? How could I change it?

Any help would be appreciated, thank you!

df2 <- data.frame(supp=rep(c("Media", "Mediana"), each=3),
                  Anno=rep(c("2019", "2020a", "2020b"),2),
                  Reddito=c(40100, 39000, 38000, 34000, 33000, 32000))
df3<-data.frame(supp=rep(c("Numero indice media", "Numero indice mediana"), each=3),
                Anno=rep(c("2019", "2020a", "2020b"),2),
                Reddito=c(100, 97, 96, 100, 96, 95))

highchart() %>% 
  hc_yAxis_multiples(
    list(title = list(text = "xxx"),opposite=F),
         list(title = list(text = "yyy"),opposite=TRUE),
    list(lineWidth = 0),
    list(showLastLabel = F, opposite = T))%>% 
  hc_add_series(data = df2,type="column" ,hcaes(x = "Anno", y = 'Reddito', group = 'supp')) %>% 
  hc_add_series(data = df3, type = "spline", hcaes(x = 'Anno', y = 'Reddito', group = 'supp'),yAxis = 1)


Solution

  • To answer your first question, try this:

    library(highcharter)
    highchart() %>% 
      hc_yAxis_multiples(
        list(title=list(text="xxx",margin = 20),
             lineWidth = 3,showLastLabel = FALSE,opposite = FALSE),
        list(
          title=list(text="yyy", margin = 20),
          lineWidth = 3,showLastLabel = FALSE,  opposite = T)
      ) %>% 
      hc_add_series(data = df2,type="column" ,hcaes(x = "Anno", y = 'Reddito', group = 'supp')) %>% 
      hc_add_series(data = df3, type = "spline", hcaes(x = 'Anno', y = 'Reddito', group = 'supp'),yAxis = 1)
    

    Values of xAxis aren't displayed in the way you expect, because you didn't define xAxis type, so they appear as index numbers by default. If you specify the type within hc_xAxis to category, it should work fine. Here's the final code:

    library(highcharter)
    
    highchart() %>% 
      hc_yAxis_multiples(
        list(title=list(text="xxx",margin = 20),
             lineWidth = 3,showLastLabel = FALSE,opposite = FALSE),
        list(
          title=list(text="yyy", margin = 20),
          lineWidth = 3,showLastLabel = FALSE,  opposite = T)
      ) %>% 
      # Specify Type
      hc_xAxis(type = "category") %>% 
      hc_add_series(data = df2,type="column" ,hcaes(x = "Anno", y = 'Reddito', group = 'supp')) %>% 
      hc_add_series(data = df3, type = "spline", hcaes(x = 'Anno', y = 'Reddito', group = 'supp'),yAxis = 1)