1

I am trying to create a grouped highcharts barchart where the bars are arranged alphabetically.

The example below shows what I am trying to do:

library(highcharter)

#create dataframe pokemon
pokemon <- pokemon <- structure(list(type_1 = c("poison", "poison", "poison", "poison", 
                                     "rock", "rock", "rock", "rock", "rock", "rock", "rock", "rock", 
                                     "rock", "poison", "rock", "rock", "rock", "rock", "rock", "rock", 
                                     "rock", "rock", "rock", "rock", "rock", "poison", "poison", "poison", 
                                     "poison", "poison", "poison", "rock", "rock", "rock", "rock", 
                                     "rock", "rock", "poison", "poison", "rock", "rock", "rock", "rock", 
                                     "rock")
                          , type_2 = c("ground", "ground", "flying", "flying", "ground", 
                                       "ground", "ground", "ground", "water", "water", "water", "water", 
                                       "flying", "flying", "ground", "ground", "dark", "psychic", "psychic", 
                                       "grass", "grass", "bug", "bug", "steel", "steel", "dark", "dark", 
                                       "bug", "dark", "fighting", "fighting", "steel", "flying", "flying", 
                                       "fighting", "water", "water", "water", "dragon", "dragon", "dragon", 
                                       "ice", "ice", "fairy"))
                     , class = c("tbl_df", "tbl", "data.frame"
                                                         )
                     , row.names = c(NA, -44L))

# create chart
pokemon%>% 
  group_by(type_1, type_2) %>% 
  summarise(n = n())%>%
  arrange(type_2)%>%
  hchart(type = "bar", hcaes(x = type_2, group = type_1, y = n))

But the arrange does not work, the bars are not ordered by type_2.

It does work if I don't use a grouping variable in my hchart:

pokemon%>%
  group_by(#type_1, 
    type_2) %>% 
  summarise(n = n())%>%
  arrange(type_2)%>%
  hchart(type = "bar", hcaes(x = type_2#, group = type_1
                             , y = n))

Is there any way to order my bars while using a grouping variable?

I am using R 3.5.2

user10781624
  • 141
  • 1
  • 13
  • Can you make this [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by directly providing the data (eg using `dput(pokemon)` or a minimal version of it) in your question? As it stands people who don't have your data can't run your code to try to modify it and give an answer. – CrunchyTopping Aug 29 '19 at 17:27
  • The pokemon dataset is included in highcharter, so if that package is loaded the data should also be available? – user10781624 Aug 30 '19 at 07:49
  • I cannot run your code, I get this error: "Error in type_1 %in% c("poison", "rock", "fighting") : object 'type_1' not found In addition: Warning messages: 1: In data.matrix(data) : NAs introduced by coercion 2: In data.matrix(data) : NAs introduced by coercion..." As far as I see, your question is not closely related to Highcharts but R and filtering data, right? – raf18seb Aug 30 '19 at 09:06
  • I'm sorry, I thought this would work. I have added a sample of the data to create a set you can all use. My question is related to highcharter in R, since I would like to know how to order a highcharts bar chart created in R. ;) – user10781624 Aug 30 '19 at 09:14
  • oops yep you're right. – CrunchyTopping Aug 30 '19 at 11:54

1 Answers1

2

I think I've already found the solution by using highcharts() and hc_add_series, this does allow for arranging:

pokemon <- pokemon %>% 
  group_by(type_1, type_2) %>% 
  summarise(n = n())%>%
  arrange(type_2)

# create chart
highchart() %>% 
  hc_add_series(pokemon, type = "bar", hcaes(x = type_2, group = type_1, y = n)) %>% 
  hc_xAxis(categories = pokemon$type_2)
user10781624
  • 141
  • 1
  • 13