Draw Slope Chart in Power BI: Part 8

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

slopechart

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]:

data

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 :

p<-ggplot(dataset)

in next step I am going to use function geom_segment

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 :

geom_segment

 

 

Still I need to refinethe chart.

Gemo_vline()

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”

 

 

geom_vline

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

up-down

 

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

labs()

labes

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

scales

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 :

left_label

geom_text()

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:

left_lable_geom_text

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.

[1]https://www.packtpub.com/mapt/book/big_data_and_business_intelligence/9781783989508/5/ch05lvl1sec58/generating-a-slope-chart

[2]https://www.gfmag.com/global-data/economic-data/countries-highest-gdp-growth

[3]http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail
rssyoutuberssyoutube
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 *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">