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 on LinkedinLeila Etaati on TwitterLeila Etaati on Youtube
Leila Etaati
Trainer, Consultant, Mentor
Leila is the first Microsoft AI MVP in New Zealand and Australia, She has Ph.D. in Information System from the University Of Auckland. She is the Co-director and data scientist in RADACAD Company with more than 100 clients in around the world. She is the co-organizer of Microsoft Business Intelligence and Power BI Use group (meetup) in Auckland with more than 1200 members, She is the co-organizer of three main conferences in Auckland: SQL Saturday Auckland (2015 till now) with more than 400 registrations, Difinity (2017 till now) with more than 200 registrations and Global AI Bootcamp 2018. She is a Data Scientist, BI Consultant, Trainer, and Speaker. She is a well-known International Speakers to many conferences such as Microsoft ignite, SQL pass, Data Platform Summit, SQL Saturday, Power BI world Tour and so forth in Europe, USA, Asia, Australia, and New Zealand. She has over ten years’ experience working with databases and software systems. She was involved in many large-scale projects for big-sized companies. She also AI and Data Platform Microsoft MVP. Leila is an active Technical Microsoft AI blogger for RADACAD.

Leave a Reply