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.
Please refer to the below screenshot.
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.
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.
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.
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!
Add the R script visual in Power BI page.
Drag the fields within R visuals.
Open the code window and click on "Edit script in external editor".
We have used the following two packages in the development of the Gantt Chart.
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.
Paste the following code.
# Convert timestamp to POSIXct and perform sort.
dataset$`Start Time`<-as.POSIXct(dataset$`Start Time`,format="%Y-%m-%dT%H:%M:%OS", tz ="GMT")
dataset$`End Time`<-as.POSIXct(dataset$`End Time`,format="%Y-%m-%dT%H:%M:%OS", tz="GMT")
ggplot(dataset,aes(x=dataset$`Start Time`, y=dataset$Group, color= dataset$Group)) +
geom_segment(aes(x=dataset$`Start Time`,xend=dataset$`End Time`,yend=dataset$Group),size=15) +
scale_x_datetime(breaks = date_breaks("2 hour"), labels=date_format("%H:%M"))+
ggtitle("Rescue by Start Time and End Time") + xlab("Time") + ylab("Group Name") + theme_bw()
Select all the code and click the "Run" button.
We can see the output in the tab of Plots. It is showing the following type of Gantt Chart.
Copy the same code and place the code within the R Editor in Power BI and click on "Run" button.
The R visual will look like following.
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.
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.