The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
For a few years now, it has become very to create interactive maps with R thanks to the package leaflet
by the Rstudio team. Nevertheless, it only provides only a few functions to create basic shapes on a map, so the information that can be represented on a single map is limited: if you have some data associated to some points, you can only represent at most two variables by drawing circles and changing their radius and color according to data.
leaflet.minicharts
is an R package that provides two functions to add and update small charts on an interactive maps created with the package leaflet
. These charts can be used to represent as many variables as desired associated to geographical points. Currently, three types of chart are supported: barcharts (the default), pie charts and polar area charts.
let’s have a look to a concrete example.
The package provides a table that contains the electric production, consumption and exchanges of France from january 2010 to february 2017 and of 12 french regions from january 2013 to february 2017.
In addition to the total production, the table contains one column for each type of production. The table also contains the latitude and longitude of the center of the regions.
library(leaflet.minicharts)
data("eco2mix")
head(eco2mix)
## area lng lat month total nuclear coal fuel gaz
## 1 Auvergne-Rhone-Alpes 4.537338 45.51266 2015-03 11430 8230 6 40 200
## 2 Auvergne-Rhone-Alpes 4.537338 45.51266 2015-06 10056 7200 4 35 5
## 3 Auvergne-Rhone-Alpes 4.537338 45.51266 2013-04 10532 7410 0 31 3
## 4 Auvergne-Rhone-Alpes 4.537338 45.51266 2015-04 10103 7275 4 39 86
## 5 Auvergne-Rhone-Alpes 4.537338 45.51266 2014-08 9052 6357 0 25 -1
## 6 Auvergne-Rhone-Alpes 4.537338 45.51266 2015-11 9258 7192 5 45 250
## hydraulic wind solar bioenergy load balance export import balanceUK balanceES
## 1 2737 74 57 82 6326 4747 NA NA NA NA
## 2 2606 44 101 58 4848 4891 NA NA NA NA
## 3 2948 55 39 42 NA NA NA NA NA NA
## 4 2487 66 81 61 5283 4439 NA NA NA NA
## 5 2487 38 76 68 4299 4447 NA NA NA NA
## 6 1563 75 40 86 5829 3141 NA NA NA NA
## balanceIT balanceCH balanceDEBE
## 1 NA NA NA
## 2 NA NA NA
## 3 NA NA NA
## 4 NA NA NA
## 5 NA NA NA
## 6 NA NA NA
Nowadays, France has an objective of 23% of renewable energies in the consumption of the country by 2020. Are the country close to its objective. Is the share of renewable energies similar in all regions?
To answer this question let us focus on the year 2016 We first prepare the required data with package dplyr
:
library(dplyr)
<- eco2mix %>%
prod2016 mutate(
renewable = bioenergy + solar + wind + hydraulic,
non_renewable = total - bioenergy - solar - wind - hydraulic
%>%
) filter(grepl("2016", month) & area != "France") %>%
select(-month) %>%
group_by(area, lat, lng) %>%
summarise_all(sum) %>%
ungroup()
head(prod2016)
## # A tibble: 6 x 23
## area lat lng total nuclear coal fuel gaz hydraulic wind solar
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Auvergne-R~ 45.5 4.54 108517 75002 59 101 2318 28398 849 803
## 2 Bourgogne-~ 47.2 4.81 2762 0 0 1 644 939 773 211
## 3 Bretagne 48.2 -2.84 3141 0 0 9 543 581 1472 192
## 4 Centre-Val~ 47.5 1.69 78430 75733 0 0 274 120 1616 238
## 5 Grand-Est 48.7 5.61 107755 82734 1650 41 8282 8990 4917 462
## 6 Hauts-de-F~ 50.0 2.77 45593 31222 48 17 8302 8 4851 122
## # ... with 12 more variables: bioenergy <dbl>, load <dbl>, balance <dbl>,
## # export <dbl>, import <dbl>, balanceUK <dbl>, balanceES <dbl>,
## # balanceIT <dbl>, balanceCH <dbl>, balanceDEBE <dbl>, renewable <dbl>,
## # non_renewable <dbl>
We also create a base map that will be used in all the following examples
library(leaflet)
<- "http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}"
tilesURL
<- leaflet(width = "100%", height = "400px") %>%
basemap addTiles(tilesURL)
We now add to the base map a pie chart for each region that represents the share of renewable energies. We also change the width of the pie charts so their area is proportional to the total production of the corresponding region.
<- c("#4fc13c", "#cccccc")
colors
%>%
basemap addMinicharts(
$lng, prod2016$lat,
prod2016type = "pie",
chartdata = prod2016[, c("renewable", "non_renewable")],
colorPalette = colors,
width = 60 * sqrt(prod2016$total) / sqrt(max(prod2016$total)), transitionTime = 0
)
We can see that the three south east regions exceed the target of 23%, but most regions are far from this objective. Globally, renewable energies represented only 19% percent of the production of 2016.
Now let’s represent the different types of renewable production using bar charts.
<- prod2016 %>% select(hydraulic, solar, wind)
renewable2016 <- c("#3093e5", "#fcba50", "#a0d9e8")
colors %>%
basemap addMinicharts(
$lng, prod2016$lat,
prod2016chartdata = renewable2016,
colorPalette = colors,
width = 45, height = 45
)
Hydraulic production is far more important than solar and wind. Without surprise, solar production is more important in south while wind production is more important in the north.
leaflet.minicharts
has been designed to represent multiple variables at once, but you still may want to use it to represent a single variable. In the next example, we represent the total load of each french region in 2016. When data passed to addMinicharts
contains a single column, it automatically represents it with circle which area is proportional to the corresponding value. In the example we also use the parameter showLabels
to display rounded values of the variable inside the circles.
%>%
basemap addMinicharts(
$lng, prod2016$lat,
prod2016chartdata = prod2016$load,
showLabels = TRUE,
width = 45
)
This is nice, isn’t it?
Until now, we have only represented aggregated data but it would be nice to create a map that represents the evolution over time of some variables. It is actually easy with leaflet.minicharts
. The first step is to construct a table containing longitude, latitude, a time column and the variables we want to represent. The table eco2mix
already has all these columns. We only need to filter the rows containing data for the entire country.
<- eco2mix %>% filter(area != "France") prodRegions
Now we can create our animated map by using the argument “time”:
%>%
basemap addMinicharts(
$lng, prodRegions$lat,
prodRegionschartdata = prodRegions[, c("hydraulic", "solar", "wind")],
time = prodRegions$month,
colorPalette = colors,
width = 45, height = 45
)
Since version 0.2, leaflet.minicharts
has also functions to represent flows between points and their evolution. To illustrate this, let’s represent the evolution of electricity exchanges between France and Neighboring countries.
To do that, we use function addFlows
. It requires coordinates of two points for each flow and the value of the flow. Other arguments are similar to addMinicharts
.
data("eco2mixBalance")
<- eco2mixBalance
bal %>%
basemap addFlows(
$lng0, bal$lat0, bal$lng1, bal$lat1,
balflow = bal$balance,
time = bal$month
)
Of course, you can represent flows and minicharts on the same map!
In shiny applications, you can create nice transition effects by using functions leafletproxy
and updateMinicharts
/updateFlows
. In the server function you first need to initialize the map and the minicharts. The important thing here is to use parameter layerId
so that updateMinicharts
can know which chart to update with which values.
<- function(input, output, session) {
server # Initialize map
$mymap <- renderLeaflet(
outputleaflet() %>% addTiles() %>%
addMinicharts(lon, lat, layerId = uniqueChartIds)
) }
Then use leafletProxy()
and updateMinicharts
in your reactive code:
<- function(input, output, session) {
server # Initialize map
...
# Update map
observe({
<- getData(input$myinput)
newdata
leafletProxy("mymap") %>%
updateMinicharts(uniqueChartIds, chartdata = newdata, ...)
}) }
You can find a live example here.
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.