PowerBI.tips

Digging Deeper with R Visuals for PowerBI

September 21, 2016 By Mike Carlo
Digging Deeper with R Visuals for PowerBI

Back by popular demand, we have another great tutorial on using R visuals. There are a number of amazing visuals that have been supplied with the PowerBI desktop tool. However, there are some limitations. For example you can’t merge a scatter plot with a bar chart or with an area chart. In some cases it may be applicable to display one graph with multiple plot types.

Now, to be fair Power BI desktop does supply you with a bar chart and line chart, Kudos Microsoft, #Winning…. but, I want more.

This brings me to the need to learn R Visuals in PowerBI. I’ve been interested in learning R and working on understanding how to leverage the drawing capabilities of R inside PowerBI. Microsoft recently deployed the R Script Showcase, which has excellent examples of R scripts.

What We’re Building

R Plot in PowerBI Desktop

This is an area plot in the background, a bar chart as a middle layer and dots for each bar. The use case for this type of plot would be to plot sales by item number. Sales are in the dark blue bars, and the price is shown as the light blue dots. The area behind the bars represents a running total of all sales for all items. Thus, when you reach item number 10, the area represents 100% of all sales for all items listed.

If you want to download my R visual script included in the sample pbix file you can do so here.

Prerequisites

First you will need to make sure you have installed R on your computer. To see how to do this you can follow my earlier post about installing R from Microsoft Open R project. Once you’ve installed R open up the R console and enter the following code to install the ggplot2 package.

install.packages("ggplot2")

Install ggplot2 Code

Once complete you can close the R console and enter PowerBI Desktop.

Loading the Sample Data

First, we will acquire some data to work with. Click on the Home ribbon and then select Enter Data. You will be presented with the Create Table dialog box. Copy and paste the following table of information into the dialog box.

ItemSalesPriceCustomer
110020Customer A
27525Customer A
32030Customer A
41815Customer A
53426Customer A
61223Customer A
72022Customer A
81519Customer A
91017Customer A
10826Customer A
112021Customer B
28024Customer B
36233Customer B
41015Customer B
51226Customer B
66024Customer B
72023Customer B
81020Customer B
9816Customer B
10720Customer B

Rename your table to be titled Data Sample.

Data Sample Table

Click Load to bring in the data into PowerBI.

Creating the Cumulative Measure

Next, we will need to create a cumulative calculated column measure using DAX. On the home ribbon click the New Measure button and enter the following DAX expression.

Cumulative = CALCULATE( sum('Data Sample'[Sales] ) , FILTERS( 'Data Sample'[Customer] ) , FILTER( all( 'Data Sample' ) , 'Data Sample'[Item] <= MAX( 'Data Sample'[Item] ) ) )

This creates a column value that adds all the sales of the items below the selected row. For example if I’m calculating the cumulative total for item three, the sum() will add every item that is three and lower.

Adding the R Visual

Now, add the R visual by clicking on the R icon in the Visualizations window.

Note: There will be an approval window that will require you to enable the R script visuals. Click Enable to proceed.

Enable R Visuals

While selecting the R visual add the following columns to the Values field in the Visualization window:

  • Cumulative
  • Item
  • Price
  • Sales

Add Column Data

Note: After you add the columns to the Values the R visual renders a blank image. Additionally, there are automatic comments entered into the R Script Editor (the # sign designates a text phrase/comment).

The R Script

Next, enter the following R code into the script editor. This will tell PowerBI Desktop to load the ggplot2 library and define all the parameters for the plot. I’ve added comments to the code using # symbols.

library(ggplot2)   # include this package to use Graphing functions below

ggplot(dataset, aes(xmin=1, x=Item)) +    # Initialize ggplot function, define the x axis with Item data
 geom_ribbon(fill=c("#D7DDE2"),         # Set the color of the Area Plot,
 aes( ymin=0, ymax=Cumulative )) +     # Define the Y-Axis data
 geom_bar(fill=c("#21406D") ,             # Define the color of the Bars
 stat = "identity" ,      # Define the Statistics property of the bars - This is a required field
 width=.6 ,               # Change the bar width to 60% - 1 would be full bar width
 aes( x=Item, y=Sales )) +          # Define the X and Y axis for bars
 geom_point( color=c("#809FFF"),    # Define the color of the dots
 size=4,       # Define the dot size
 aes( x=Item, y=Price )) +          # Define the X and Y axis values
 theme_classic(base_size=18) +      # Remove unwanted items from plot area such as grid lines and X and Y axis lines, Change font size to 18
 theme( axis.title.x = element_text(colour = "dark grey"),     # Define the X axis text color
 axis.title.y = element_text(colour = "dark grey")) +          # Define the Y axis text color
 labs( x="Item Number", y="Sales")                             # Define the labels of the X and Y Axis

Press the execute R Script button which is located on the right side of the R Script Editor bar.

Execute R Script Editor Button

The R Script will execute and the plot will be generated.

R Plot Generation

Why This is Powerful

Great, we have completed an R visual. So what, why is this such a big deal? Well, it is because the R Script will execute every time a filter is applied or changed. Let’s see it in action.

Add a slicer with the Customer column.

Add Customer Slicer

Notice when you select the different customers, either A or B, the R script Visual will change to reflect the selected customer.

Customer B Selected

Now you can write the R script code once and use the filtering that is native in PowerBI to quickly change the data frame supporting the R Visuals.

As always, thanks for following along. Don’t forget to share if you liked this tutorial.

Previous

Using R Visuals in Power BI

More Posts

Feb 25, 2026

Excel vs. Field Parameters – Ep. 505

Mike and Tommy debate the implications of AI on app development and data platforms, then tackle a mailbag question on whether field parameters hinder Excel compatibility in semantic models. They explore building AI-ready models and the future of report design beyond Power BI-specific features.

Feb 20, 2026

Living in a Direct Lake World – Ep. 504

Mike and Tommy dive deep into what it means to live in a Direct Lake world, exploring the practical realities of building semantic models on top of Fabric lakehouses. They cover the trade-offs, gotchas, and best practices for teams making the shift from Import mode to Direct Lake.

Feb 18, 2026

Hiring the Report Developer – Ep. 503

Mike and Tommy unpack what a report developer should know in 2026 — from paginated reports and the SSRS migration trend to the line between report building and data modeling.