1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| data("gapminder", package = "gapminder")
gapminder %>% dplyr::filter(year == 2007) %>% mutate( gdpPercap = log(gdpPercap), pop = sqrt(pop / pi) / 1500 ) %>% apex(mapping = aes(gdpPercap, lifeExp, z = pop, group = continent, label = country), type = "scatter") %>% ax_chart(zoom = list( enabled = TRUE, type = "xy" )) %>% ax_yaxis( decimalsInFloat = 0, axisBorder = list(show = TRUE), axisTicks = list(show = TRUE), title = list(text = "life expectancy at birth (in years)") ) %>% ax_xaxis( tickAmount = 8, labels = list( formatter = JS("function(val) {return val.toFixed(2);}") ), tooltip = list(enabled = FALSE), title = list(text = "GDP per capita (log-scale)") ) %>% ax_grid(xaxis = list(lines = list(show = TRUE))) %>% ax_legend(position = "right", offsetY = 70) %>% ax_markers(hover = list(sizeOffset = 0, size = 25)) %>% ax_tooltip(custom = JS(paste( "function({ series, seriesIndex, dataPointIndex, w }) {", "console.log(w); return (", "'<div>' +", "'<div class = \"apexcharts-tooltip-title\">' +", "w.config.series[seriesIndex].data[dataPointIndex].label", "+ '</div>' +", "'<div style = \"padding: 5px;\">' +", "'<div class = \"apexcharts-tooltip-y-group\">' +", "'<span class = \"apexcharts-tooltip-text-label\">' +", "'Population: ' +", "'</span>' +", "'<span class = \"apexcharts-tooltip-text-value\">' +", "Math.round(Math.pow(w.config.series[seriesIndex].data[dataPointIndex].z * 1500, 2) * Math.PI). toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, \",\") +", "'</span>' +", "'</br>' +", "'<span class = \"apexcharts-tooltip-text-label\">' +", "'GDP per capita: ' +", "'</span>' +", "'<span class = \"apexcharts-tooltip-text-value\">' +", "Math.round(Math.exp(w.config.series[seriesIndex].data[dataPointIndex].x)) +", "'</span>' +", "'</br>' +", "'<span class = \"apexcharts-tooltip-text-label\">' +", "'Life expectancy: ' +", "'</span>' +", "'<span class = \"apexcharts-tooltip-text-value\">' +", "w.config.series[seriesIndex].data[dataPointIndex].y +", "'</span>' +", "'</div>' +", "'</div>' +", "'</div>'", ");", "}", sep = "\n" ))) %>% ax_labs( title = "Life expectancy, GDP and population", subtitle = "gapminder dataset from {gapminder}" ) %>% ax_title( style = list(fontSize = "22px") ) %>% ax_subtitle( style = list(fontSize = "16px", color = "#BDBDBD") )
|