Draw Slope Chart in Power BI: Part 8

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”

library(ggplot2)
library(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 :

library(ggplot2)
library(scales)
theme_set(theme_classic())

p <- ggplot(dataset) + geom_segment(aes(x=1, xend=2, y=dataset$GPD2014, yend=dataset$GPD2015, col=dataset$Status), size=.75, show.legend=F)

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”

library(ggplot2)
library(scales)
theme_set(theme_classic())

p <- ggplot(dataset) + geom_segment(aes(x=1, xend=2, y=dataset$GPD2014, yend=dataset$GPD2015, col=dataset$Status), size=.75, show.legend=F) + 
                  geom_vline(xintercept=1, linetype="dashed", size=.1) + 
                  geom_vline(xintercept=2, linetype="dashed", size=.1)

 

 

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:

library(ggplot2)
library(scales)
theme_set(theme_classic())

p <- ggplot(dataset) + geom_segment(aes(x=1, xend=2, y=dataset$GPD2014, yend=dataset$GPD2015, col=dataset$Status), size=.75, show.legend=F) + 
                  geom_vline(xintercept=1, linetype="dashed", size=.1) + 
                  geom_vline(xintercept=2, linetype="dashed", size=.1) +
                  scale_color_manual(labels = c("Up", "Down"), values = c("#00ba38", "red"))

 

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()

labs(x="", y="GPD")

labes

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

# Original Script. Please update your script content here and once completed copy below section back to the original editing window #
library(ggplot2)
library(scales)
theme_set(theme_classic())

p <- ggplot(dataset) + geom_segment(aes(x=1, xend=2, y=dataset$GPD2014, yend=dataset$GPD2015, col=dataset$Status), size=.75, show.legend=F) + 
                  geom_vline(xintercept=1, linetype="dashed", size=.1) + 
                  geom_vline(xintercept=2, linetype="dashed", size=.1) +
                  scale_color_manual(labels = c("Up", "Down"), values = c("#00ba38", "red"))+ labs(x="", y="GPD") +  # Axis labels
                  xlim(0.5, 2.5) + ylim(0,(1.1*(max(dataset$GPD2014, dataset$GPD2015))))

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

left_label <- paste(dataset$Countries, dataset$GPD2014,sep=", ")
right_label <- paste(dataset$Countries, dataset$GPD2015,sep=", ")

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

p <- p + geom_text(label=left_label, y=dataset$GPD2014, x=rep(1, NROW(dataset)), hjust=1.1, size=3.5)

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

library(ggplot2)
library(scales)
theme_set(theme_classic())

p <- ggplot(dataset) + geom_segment(aes(x=1, xend=2, y=dataset$GPD2014, yend=dataset$GPD2015, col=dataset$Status), size=.75, show.legend=F) + 
                  geom_vline(xintercept=1, linetype="dashed", size=.1) + 
                  geom_vline(xintercept=2, linetype="dashed", size=.1) +
                  scale_color_manual(labels = c("Up", "Down"), values = c("#00ba38", "red"))+ labs(x="", y="GPD") +  # Axis labels
                  xlim(0.5, 2.5) + ylim(0,(1.1*(max(dataset$GPD2014, dataset$GPD2015))))  # X and Y axis limits

left_label <- paste(dataset$Countries, dataset$GPD2014,sep=", ")
right_label <- paste(dataset$Countries, dataset$GPD2015,sep=", ")
# Add texts
p <- p + geom_text(label=left_label, y=dataset$GPD2014, x=rep(1, NROW(dataset)), hjust=1.1, size=3.5)
p <- p + geom_text(label=right_label, y=dataset$GPD2015, x=rep(2, NROW(dataset)), hjust=-0.1, size=3.5)
p <- p + geom_text(label="Time 1", x=1, y=1.1*(max(dataset$GPD2014, dataset$GPD2015)), hjust=1.2, size=5)  # title
p <- p + geom_text(label="Time 2", x=2, y=1.1*(max(dataset$GPD2015, dataset$GPD2015)), hjust=-0.1, size=5)  # title


p

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

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