Reputation: 189
I am trying to replicate the above graph using the highcharter library in R. I am using this as API - reference [https://codepen.io/pen/]
Here is the dput() of my data
dput(country_totals2)
structure(list(country_name = c("Norway",
"United States", "Germany", "Canada", "Austria"), level = c("Total",
"Total", "Total", "Total", "Total"), `Total net production` = c(69212.347,
81967.627, 571799.713, 551685.823, 280234), Imports = c(21635.908,
11573.411, 31701.359, 12826.095, 47179), Exports = c(4308.347,
25480.503, 80187.327, 76094.985, 3270), `Energy absorbed by pumping` = c(1347.901,
1372.884, 8347.706, 7331.317, 2233), `Energy supplied` = c(85192.007,
66687.651, 514966.039, 481085.616, 321910)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
Below is my R code -
highchart() %>%
hc_chart(type = "column",
inverted = TRUE,
polar = TRUE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_xAxis(categories= list('Norway <span class="f16"><span id="flag" class="flag no">' ,
'United States <span class="f16"><span id="flag" class="flag us">' ,
'Germany <span class="f16"><span id="flag" class="flag de">' ,
'Canada <span class="f16"><span id="flag" class="flag ca">' ,
'Austria <span class="f16"><span id="flag" class="flag at">' ,
'</span></span>')) %>%
hc_yAxis( tickLength = 0,
gridLineColor = 'transparent'
) %>%
hc_xAxis( tickLength = 0,
gridLineColor = 'transparent'
)%>%
hc_add_series( data = country_totals2$Imports) %>%
hc_add_series( data = country_totals2$Exports) %>%
hc_add_series(data = country_totals2$`Energy absorbed by pumping`) %>%
hc_add_theme(hc_theme_ffx())
The code is working fine except the country flags as indicated in the 'hc_xAxis
' commands isnt appearing.
How should I modify the JS/HTML code in the hc_xAxis
command for the flags to appear in R?
Upvotes: 1
Views: 345
Reputation: 24252
The original polar bar chart is available here.
Digging into the code, one can find the following lines concerning the flag service:
<!-- Flag sprites service provided by Martijn Lafeber,
https://github.com/lafeber/world-flags-sprite/blob/master/LICENSE -->
<link rel="stylesheet" href="https://github.com/downloads/lafeber/world-flags-sprite/flags16.css" />
which need to be prepended to the highchart plot using htmlwidgets::prependContent
.
library(highcharter)
library(purrr)
library(htmlwidgets)
library(htmltools)
country_totals2 <- structure(list(country_name = c("Norway",
"United States", "Germany", "Canada", "Austria"), level = c("Total",
"Total", "Total", "Total", "Total"), `Total net production` = c(69212.347,
81967.627, 571799.713, 551685.823, 280234), Imports = c(21635.908,
11573.411, 31701.359, 12826.095, 47179), Exports = c(4308.347,
25480.503, 80187.327, 76094.985, 3270), `Energy absorbed by pumping` = c(1347.901,
1372.884, 8347.706, 7331.317, 2233), `Energy supplied` = c(85192.007,
66687.651, 514966.039, 481085.616, 321910)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
p <- highchart() %>%
hc_chart(type = "column",
inverted = TRUE,
polar = TRUE) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_xAxis(categories= c("Norway","United States","Germany","Canada","Austria"),
labels=list(useHTML=TRUE,
formatter = JS("function() {
return {
'Norway': 'Normay <span class=\x22 f16\x22><span class=\x22 flag no \x22></span></span> ',
'United States': 'United States <span class=\x22 f16\x22><span class=\x22 flag us \x22></span ></span> ',
'Germany': 'Germany <span class=\x22 f16\x22><span class=\x22 flag de \x22></span ></span> ',
'Canada': 'Canada <span class=\x22 f16\x22><span class=\x22 flag ca \x22></span ></span> ',
'Austria': 'Austria<span class=\x22 f16\x22><span class=\x22 flag at\x22></span ></span> '
}[this.value];
}")
)
) %>%
hc_yAxis( tickLength = 0,
gridLineColor = 'transparent'
) %>%
hc_xAxis( tickLength = 0,
gridLineColor = 'transparent'
)%>%
hc_add_series( data = country_totals2$Imports) %>%
hc_add_series( data = country_totals2$Exports) %>%
hc_add_series(data = country_totals2$`Energy absorbed by pumping`) %>%
hc_add_theme(hc_theme_ffx())
prependContent(p, HTML('<link rel="stylesheet" href="https://github.com/downloads/lafeber/world-flags-sprite/flags16.css" />'))
Upvotes: 2