Reader small image

You're reading from  Extending Power BI with Python and R - Second Edition

Product typeBook
Published inMar 2024
Reading LevelIntermediate
PublisherPackt
ISBN-139781837639533
Edition2nd Edition
Languages
Right arrow
Author (1)
Luca Zavarella
Luca Zavarella
author image
Luca Zavarella

Luca Zavarella has a rich background as an Azure Data Scientist Associate and Microsoft MVP, with a Computer Engineering degree from the University of L'Aquila. His decade-plus experience spans the Microsoft Data Platform, starting as a T-SQL developer on SQL Server 2000 and 2005, then mastering the full suite of Microsoft Business Intelligence tools (SSIS, SSAS, SSRS), and advancing into data warehousing. Recently, his focus has shifted to advanced analytics, data science, and AI, contributing to the community as a speaker and blogger, especially on Medium. Currently, he leads the Data & AI division at iCubed, and he also holds an honors degree in classical piano from the "Alfredo Casella" Conservatory in L'Aquila.
Read more about Luca Zavarella

Right arrow

Advanced Visualizations

As you saw in Chapter 19, Exploratory Data Analysis, you can create very professional-looking graphs using the ggplot2 package and its extensions. In this chapter, you’ll learn how to create advanced and attractive custom graphs, with a special focus on circular barplots. Circular barplots are particularly useful for showing relationships in data that are periodic or cyclical in nature, providing a visually appealing and space-efficient way to display such patterns. They are ideal for comparing categories that are parts of a whole, such as hours in a day or months in a year, where the circular layout emphasizes the cyclical nature.

The visualization of such plots will be done exclusively using R programming, as it offers robust support for creating these intricate graphics. This is because, despite Python’s extensive capabilities with libraries like matplotlib, it is quite challenging to build circular plots with it. In addition, plotnine...

Technical requirements

This chapter requires you to have a working internet connection and Power BI Desktop already installed on your machine (version: 2.121.644.0 64-bit, September 2023). You must have properly configured the R engine and IDEs as outlined in Chapter 2, Configuring R with Power BI.

Choosing a circular barplot

Very often, we need to display the measures associated with different categorical entities using a bar chart (or barplot). However, when the number of entities to be represented exceeds 15 or 20, the graph begins to become unreadable, even if it is arranged vertically:

A graph with a bar graph  Description automatically generated with medium confidence

Figure 21.1: A barplot of worldwide weapons sellers

In this case, as you saw in Chapter 19, Exploratory Data Analysis, it is often a good idea to plot a maximum number of entities and then group the subsequent entities into a single category (in our case, the Others category). This preserves the readability of the graph, but loses some of the information you want to represent.

If it is absolutely necessary to display all entities with all their dimensions, we often resort to a more eye-catching organization of the space occupied by the barplot, wrapping it in a circular shape, thus obtaining a circular barplot:

A graph with text on it  Description automatically generated with medium confidence

Figure 21.2: Circular barplot of worldwide weapons sellers...

Implementing a circular barplot in R

In this section, you’ll find R code inspired by the code available to the entire R community at the R Graph Gallery website (https://www.r-graph-gallery.com/index.html). Along with some minor additions, we’ve refactored and generalized this code into a function called circular_grouped_barplot(). This was done using the tidy evaluation framework (see the included references for more information). The main benefit of this change is that our function can now handle any dataset.

You may recall from previous chapters that when dealing with R functions, column names were passed to functions as strings. However, with the advent of tidy evaluation, these column names can now be passed using tidyverse grammar instead. Essentially, this allows column names to be passed directly through a pipeline. Consider the example below for a better understanding:

circular_grouped_barplot(data = speakers_tbl,
                         grp_col_name...

Implementing a circular barplot in Power BI

As you have already seen in the previous chapters, Power BI is able to render graphs developed with ggplot2 using R visuals. So, no matter how complex the graph you created with ggplot2 is, you can be sure that Power BI will handle it well.

To create a circular barplot in Power BI, follow these steps:

  1. Make sure that Power BI Desktop is pointing to the version of CRAN R that is dedicated to R visuals in Options.
  2. Click on Get data, and then click Text/CSV.
  3. Select the Scores.csv file located in the Chapter 21 folder and click Open.
  4. You will see a preview of the CSV file. Be sure to select 65001: Unicode (UTF-8) for File Origin. This will ensure that special characters in speaker names are displayed correctly. Then, click on Load.
  5. Click on the R Visual icon in the Visualizations panel, enable it, and then resize the visual to fit the entire available canvas.
  6. With the R visual selected, expand the...

Summary

In this chapter, you learned how to create sophisticated and visually appealing custom graphs, with a particular focus on circular barplots. First, the merits of circular barplots were discussed, especially when dealing with large numbers of entities or categories. From there, you learned the ins and outs of creating these barplots in R, using the ggplot2 package and the tidy evaluation framework.

In addition, this chapter explored how to integrate R scripts into Power BI to render complex ggplot2 graphs. It provided a step-by-step walk-through for importing data into Power BI, creating an R visual, and then customizing the circular barplot within that environment.

In the next chapter, you’ll learn how to develop interactive custom visuals in R and Power BI.

References

For additional reading, check out the following articles:

Test your knowledge

  1. Why are circular barplots particularly useful?
  2. What is the recommended approach for utilizing column names in R functions, emphasizing the direct use of column names instead of passing them as strings?
  3. What is the meaning of the coord_polar() function in ggplot2?

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://discord.gg/MKww5g45EB

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Extending Power BI with Python and R - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781837639533
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Luca Zavarella

Luca Zavarella has a rich background as an Azure Data Scientist Associate and Microsoft MVP, with a Computer Engineering degree from the University of L'Aquila. His decade-plus experience spans the Microsoft Data Platform, starting as a T-SQL developer on SQL Server 2000 and 2005, then mastering the full suite of Microsoft Business Intelligence tools (SSIS, SSAS, SSRS), and advancing into data warehousing. Recently, his focus has shifted to advanced analytics, data science, and AI, contributing to the community as a speaker and blogger, especially on Medium. Currently, he leads the Data & AI division at iCubed, and he also holds an honors degree in classical piano from the "Alfredo Casella" Conservatory in L'Aquila.
Read more about Luca Zavarella