by Phoebe Parker – Consultant, Altis Melbourne
Recently, a client asked if it was possible to customise the axis on a line graph in Power BI, as there was no option for it in any menus. We did not want to change the chart type, as it was still the best way to visualise our data, so we had to think outside the box. The easiest solution was to write a short R script to display the chart instead.
Using R in Power BI is relatively simple, you just need R and your packages installed on your machine. The package used in this solution is ‘ggplot2’.
‘ggplot2’ is one of the most popular packages in R and is used for data visualisations. It breaks graphs up into semantic components (scales, layers, etc). In most cases, you start with ggplot()
, and state your dataset and x and y variables. You then add on layers (such as geom_line()
, geom_point()
), scales (scale_colour_brewer()
) and co-ordinate systems (coord_flip()
).
The dataset we will be using is:
Time | Total_bill |
Breakfast | 13.45 |
Lunch | 20.80 |
To draw a basic line graph, we use ggplot
to specify the data we are using, and geom_line
to put it into a line graph.
From there, we can keep adding layers onto it.
We can also add titles and change the axis range. In this example, we have created a vector of the y-axis labels that we want, you can also specify a range and let R choose where to put the labels. If you do not specify an upper limit, then the maximum will be the highest y value in your dataset.
All the functions that have been used can be mixed and matched as you choose, there is a handy cheat sheet that lists them all.
To use all this in Power BI, after you have selected the data you wish to use, click on the R logo in the visualisations tile.
Power BI will create your dataset for you, so you can begin your script by calling the package that you need. Then write your ggplot code!
There are many other useful R functions that can be utilised in Power BI, such as data smoothing, clustering and interactivity.
Want some help with Data Visualisation or Power BI? Contact one of our offices across Australia, NZ and UK. Connect with Altis
If you are interested in reading more about Power BI check out these blogs: