Dynamic colored R Diagram in Power BI using Earthtone
4. October 2016 Leave a comment
Power BI integrates R to perform complex analysis and sophisticated visualization. Earthtones is an R library which takes a screenshot from Google Maps of certain geo coordinate and extracts the landscape colors. Earthtones can be used to color diagrams based on the local color schema.
Earthtones
The package can be found on github. There is also a description how to donwload and install the package. Using earthtones is easy. The function get_earththones takes the parameters longitude and latitude, zoom and the number of colors to extract. The earthtones for Steyr look like this:
get_earthtones(latitude=48.045,longitude=14.422,zoom=15,number_of_colors=8)
Power BI Data Model
The data model in this example is very simple. There are two excel sheets, one for the revenue by city and item group, another for the geo coordinates (longitude / latitude) and optimal zoom level per city.
The Power BI model is very simple, both data sources are linked by the city name
R Boxplot diagram in Power BI
In this example a simple boxplot is used to visualize the revenue by item group. A data slicer for the column city is used to filter the data. The R diagram takes the following columns as input:
- Longitude
- Latitude
- Zoom
- City
- Price
- Group
If only one city is selected, the R script shall gather the cities earthtone colors and format the diagram. If more than one city is selected, the diagram shall be formatted in red, blue and green. The following script loads the earthtone library and gets the distinct number of city names from the dataset. If there is more than 1 distinct name in the dataset the color variable is set to red,blue,green. Otherwise, earthtone is used to get the city typical color schema.
library(earthtones)
numCities <- length(unique(dataset$Stadt))
if(numCities > 1) {
color <- c(“red”,”blue”,”green”)
} else {
color <- get_earthtones(latitude = dataset$Lat[1],
longitude=dataset$Lon[1],
zoom= dataset$Zoom[1],
number_of_colors=3,include.map=FALSE)
}boxplot(Preis~Gruppe,dataset,col=(color),ylab=”Revenue”,xlab = “Item Group”)
The R script in Power BI looks like this:
If a city is selected, for example San Francisco, the diagram is formatted in the colors blue, gray and brown.
The colors fit the blue sea, the bay and the city seen from space.
If another city, for example Cairo, is selected the diagram gets formatted in dark green, dark- and light brown.
That fits the cities local color schema, the brown buildings, the green plants along the Nile and the desert sand.