Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI

Overview

In this article, we will talk about how we can create a Gantt Chart using R script visual within the Power BI.

In Power BI, there are many custom visuals which provide flexibility to create a Gantt Chart. But my requirement was somewhat different. I have the following scenario and the following types of data.

Scenario

Please refer to the below screenshot.

 Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI

I have information for different groups with their start time and end time. I want to represent a filled area with the line (duration) for every group.

Example

If Group 1 has a duration from 1:00 AM to 3:00 AM, I want to show the highlighted area between 1:00 PM to 3:00 PM. To represent this type of duration, I have developed the Gantt Chart.

Code

I have uploaded the sample data source file and sample Power BI file on GitHub. You can directly download the code using the following URL.

Download Code

Prerequisites

Before we can proceed further, you must have the following software installed on your machine. I have already mentioned the installation process in my previous articles. I also mentioned the link with the name of the software.

Now, let’s get started!

Step 1

Add the R script visual in Power BI page.

Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI 

Step 2

Drag the fields within R visuals.

Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI 

Step 3

Open the code window and click on "Edit script in external editor".

Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI 

Step 4

We have used the following two packages in the development of the Gantt Chart.

  • ggplot2
  • scales

If R Studio doesn’t have those packages installed, install the packages.

Go to the Packages tab, click on "Install". Type the name of the package and click on the "Install" button.

Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI 

Step 5

Paste the following code.

  1. library(ggplot2)      
  2. library(scales)     
  3. # Convert timestamp to POSIXct and perform sort.    
  4. dataset$`Start Time`<-as.POSIXct(dataset$`Start Time`,format="%Y-%m-%dT%H:%M:%OS", tz ="GMT")    
  5. dataset$`End Time`<-as.POSIXct(dataset$`End Time`,format="%Y-%m-%dT%H:%M:%OS", tz="GMT")    
  6. ggplot(dataset,aes(x=dataset$`Start Time`, y=dataset$Group, color= dataset$Group)) +    
  7. geom_segment(aes(x=dataset$`Start Time`,xend=dataset$`End Time`,yend=dataset$Group),size=15) +    
  8. scale_colour_discrete(guide=guide_legend(override.aes=list(size=10))) +    
  9. scale_x_datetime(breaks = date_breaks("2 hour"), labels=date_format("%H:%M"))+    
  10. ggtitle("Rescue by Start Time and End Time") + xlab("Time") + ylab("Group Name") + theme_bw()    

Step 6

Select all the code and click the "Run" button.

Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI 

Step 7

We can see the output in the tab of Plots. It is showing the following type of Gantt Chart.

Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI 

Step 8

Copy the same code and place the code within the R Editor in Power BI and click on "Run" button.

Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI 

Step 9

The R visual will look like following.

Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI 

Step 10

Publish the report in your App Workspace. Make sure you have a Pro license activated, otherwise, you are not able to see the R script visualization.

Conclusion

This is how we can create a Gantt Chart within Power BI. Stay connected with me for these types of amazing articles in the future.


Similar Articles