Draw Slope Chart in Power BI: Part 8



Slope chart can be used for comparing data between two different time periods. it is a very easy way to depict the difference between to time, two elements or any other two attributes. The slope charts can be used to study the correlation between variables or to study the change in the same variable between two different time periods [1].

imagine we have GPD for Growth By Country as below [2]:


I am going to draw a “Slope Chart” to compare the GPD of 6 main countries together for 2014 and 2015. also, I have a second column that shows if the GPD is increasing regarding the 2014, so the status is green otherwise is red.

First I draw it on Power BI as a simple R visual then I will show how to have it as R custom Visual.

for drawing Slope Chart we need two main packages: “ggplot2” and “scales”

then, I am using the ggplot2 function to draw the dataset that I have

so :


in next step I am going to use function geom_segment


geom_segment draws a straight line between points (x, y) and (xend, yend). it shows an area with X values range from 1 to 2 and Y which starts point for GPD2014 and y end point for GPD2015, after I run the code :

I got below :




Still I need to refinethe chart.


I am going to draw a line for start of X value at 1 and end of X value at 2 using geom_vline function, this function gets the start point of X as “xintercept” and its end. with the size of the line and its type as “dashed”




however, there is a problem in above picture, the color is not reflect the increase or decrease of the GPD

I am going to use another function name “scale_color_msnusl() which able to recolr the lines for line who are in upward trend “green” and for lines who have “downward” trend “red” color. I need below code to address this need:


so I have below chart



The label that is located in y axis is not correct, so I am going to change it using below function:



Now , I am going to refine the chart a bit more, by allocationscale to both X and Y axis

so the chart will be as below


Now, chart look better, however, there is no data about the coutries and their GPD  on chart and it is not clear which line related to which country.

so I am going to create a lable by concating the “countries name” and their related GPD number  both for left and right side as below

for instance, the value for the left side would be as below :



geom_text is a function that able to put the label for y and X value in chart. so I am going to add the function to the rest of the chart. for instance for the left side we have the below code

so after running the whole code till now we have below image:


I am going to add some more texts to the right side ofthechart as well as for x lables. So the whole code would be as below

and the final image would be

finall image

in next post I will explain the other chart that weable to draw ourself in Power BI.




Leila Etaati
Dr. Leila Etaati is Principal Data Scientist, BI Consultant, and Speaker. She has over 10 years’ experience working with databases and software systems. She was involved in many large-scale projects for big sized companies. Leila has PhD of Information System department, University of Auckland, MS and BS in computer science. Leila is Microsoft Data Platform MVP.

Leave a Reply

Your email address will not be published. Required fields are marked *