Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Power BI support certain type of visuals that are by default available in the document. These are absolutely great and work perfectly fine, have a lot of capabilities to set properties and change the settings.
But every so often in past year, I have come across the fact that I wanted to visualize a typical line chart with two lines, each with it’s own Y-axis.
Let me give you a quick R example. First, very simple dataset, where there are two values, each with a slightly different scale: quantity in range from 499 to 760 and temperature in range from 15 to 24.
So what would normally happen, when we put this two ranges on same Y-axis:
# Both on the same y-axis plot(dataset[,3], type="l", col="red", lty=1, ylim=c(0,1000), ylab = "Qty and Temp") # Add second line lines(dataset[,2], col="brown",lty=1) legend("topleft", c("Temperature", "Quantity"), col = c("red", "brown"), lty = c(1, 1))
With the following resutls:
We see that the temperature has almost lost all the visible variance, where as, quantity still has some.
To deal with this, we need to shift one of the lines to right Y-axis. Following this, little additional coding:
par(mar = c(5, 5, 3, 5)) plot(dataset[, 2], type ="l", ylab = "Quantity", main = "Quantity and temperature", xlab = "Date", col = "brown") par(new = TRUE) plot(dataset[,3], type = "l", xaxt = "n", yaxt = "n", ylab = "", xlab = "", col = "red", lty = 1) axis(side = 4) mtext("temperature", side = 4, line = 3) legend("topleft", c("Quantity", "Temperature"), col = c("brown", "red"), lty = c(1, 1))
And the result is much more obvious:
And now we can see how the temperature fluctuates (for more than 10°C).
So now that we have a solution, let’s go back to Power BI.
1. Default Visual
With the same dataset, this would be “out-of-the-box” visual, available to you:
A combination of Bar chart and line chart, each with it’s own y-axis. Great. But If I want two lines, this is not possible with out of the box visuals.
2. Using R or Python
Copying the R code from above introduction example into Power BI, literally makes it the same in Power BI. Same logic goes and applies to Python.
It does the job beautifully. Where is the trick with R or Python visuals. I have a couple:
- to many times I have seen data engineers who start to use Power BI, that R or Python is just an overkill to adopt quickly,
- it takes coding to plot a graph and not everyone has a great idea how to tackle this issue, and
- publishing and deploying Power BI with R or Python on on-prem Power BI reporting server will not work with neither – R or Python – visual.
This said, there is a lot of gap for improvement.
3. Downloading custom visuals
Yes, you can download a custom visual. Go ahead
4. Building your own custom visual
This area is still under-explored among the data engineers and business intelligence people. And purpose of this post is, not only to point them to start exploring on their own, but also to show them, that is not a big deal to tinker on their own.
I have done my on this website: Charts PowerBI.
So, let’s walk through how to do it.
1. Get a sample dataset. You can download (data_twoCharts.csv) mine as well from Github.
2. Go to https://charts.powerbi.tips/ and select New.
3. Drag and drop the csv file into desired Data field, and you should get the preview of data:
4. Click Done.
5. The the consists of 1) blank canvas pane on right hand side and 2) Glyph pane, 3) data Pane and 4) Layers Pane.
The Canvas pane can hold multiple plot segments, which we will use to generate two plots, one on top of the other.
6. Drag and drop the individual data columns (from data pane) onto canvas pane. I did, first the date column, and drag it on top of X-axis (you will see, it will snap automatically on it), and Quantity on the Y-Axis.
7. Drag the Symbols in Glyph Pane. And click on Links to Create Links. This will automatically connect all the dots.
8. You should get a graph like this.
9. Great, half way done. Now resize the Plot Segment to reduce it to 50%. Click on blank canvas and Add anothoer Glyph, that will be associated with new Plot segment.
10. Add another Plot segment (remember we are building two plot graphs, one on top of each other.)
11. With new Plot segment, repeat the step 6, 7 and 8. Drag the Date ( to X-axis) and Temperature (not quantity) (to Y-axis) to canvas pane, drag Symbol to Glyph Pane and click Links to Create Links. And you should end up with:
12. On new Plot (on right hand side), we want to switch Y-axis to right side, by clickling on Layer Pane for selected Plot Segment.
13. Last part is a bit finicky. With your mouse hover over the corner of left plot (and repeat with right plot), on green point and drag it over the other Plot. Yellow dotted line will appear to denote that you are expanding the graph.
14. Once you do for both, there will be only “one” X-axis (one on top of the other), both Plot segments will be represented as layer on top of the layer.
15. Optionally, some colour coding and graph design is super desired. This can be changed in the Layers Pane. Once you are happy and satisfied with your visual, export it as Power BI custom visual:
and give it a proper name with labels for X and Y axis. These names will be visible in the Power BI document. And also, give a Custom Visual a proper name
Once you have done this, open Power BI and add it, the same way as all other additional/custom visuals:
From here on, you can use this visual with any dataset you want (it is not omitted to sample dataset you used for creating custom visual) and it is also ready to be published / deployed on on-prem Power BI Reporting server.
All code, data sample and Power BI document with custom visual are available on GitHub.
Happy PowerBI-ing.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.