Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

Dynamically place text in R Script visual

Because Power BI is designed as a tool to be used interactively, it has many limitations on static data labels. To get around this issue, I'm using ggplot in an R Script visual to create a bubble chart where I can label bubble values with multiple data dimensions.

 

The R Script visual passes fields as an R dataframe named dataset. So far, so good. I want to place annotation text on the right side of the plot. To do this, I need to know the extent of the x and y axes and use that information to set the x and y values in the geom_text() or annotate() functions. To do that, I need to know the minimum and maximum values of the x and y data points.

 

Assuming my fields are named x_data and y_data, I have included the following in my R script:

xmin <- min(dataset$x_data)

xmax <- max(dataset$x_data)

ymin <- min(dataset$y_data)

ymax <- max(dataset$y_data)

 

I then use the variables in to position my text:

g <- dataset %>%
   ggplot(aes(x  = x_data, y = y_data)) +

   geom_point(aes(size = size_data)) +

   annotate(geom='label', label='Test text', size = 4, x=xmax, y=ymax, hjust="inward", vjust="inward")

 

g

 

The code runs without error, but the text label does not appear. If I hard code the values of xmax and ymax in the annotate() command, it does. This makes it appear that the variable values are not visible to the rest of the script, yet there is no error.

 

Does anyone know if calculated R variables can work the way I'm intending? And if they can't, is there any other way to accomplish what I'm trying to do?

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

After a bit of additional analysis, I found the issue. On my Power BI page, I use a number parameter slider which feeds a topN function to limit the number of data points plotted. Since I can't pass the parameter directly as a scalar constant, I set the size of the unwanted points to null. This causes the min() and max() functions to return NA.
My solution was to filter the dataset data frame before calculating min and max for my axes.

 

Here's the code, FWIW:

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:

# dataset <- data.frame(Vendor, Market Share, Growth, Revenue YoY, Top Vendor Revenue)
# dataset <- unique(dataset)

# Paste or type your script code here:
# Load required libraries
library(tidyverse)
library(scales)
library(ggrepel)

# Filter and format dataset
dataset <- dataset %>%
    filter(!grepl('^Other ', .$Vendor )) %>%    # Remove 'Other Vendors' from output
    filter(.$`Top Vendor Revenue` > 1)     # Workaround due to fact PBI does not allow variables to be passed to R

# Compute axis limites from dataset
xmin <- 0
xmax <- max(max(dataset$`Market Share`), 0.25)
ymin <- min(dataset$`Growth`)
ymax <- max(dataset$`Growth`)

View solution in original post

3 REPLIES 3
amitchandak
Super User
Super User

@Anonymous , Make sure your calculation are done within canvas(Visual), you are not doing page on power bi page

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

@amitchandak I don't know what your reply means. I am using Power BI Desktop. I am using the built-in R script visualization. All my code is entered in the R script editor that is part of the R script visualization. The  ggplot bubble chart I have created works and displays properly. The issue is that I am trying to use a calculated variable to place a text annotation on the chart canvas, but it doesn't appear to be recognized. If I run the same code in RStudio, it does work.

Does that help?

Anonymous
Not applicable

After a bit of additional analysis, I found the issue. On my Power BI page, I use a number parameter slider which feeds a topN function to limit the number of data points plotted. Since I can't pass the parameter directly as a scalar constant, I set the size of the unwanted points to null. This causes the min() and max() functions to return NA.
My solution was to filter the dataset data frame before calculating min and max for my axes.

 

Here's the code, FWIW:

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:

# dataset <- data.frame(Vendor, Market Share, Growth, Revenue YoY, Top Vendor Revenue)
# dataset <- unique(dataset)

# Paste or type your script code here:
# Load required libraries
library(tidyverse)
library(scales)
library(ggrepel)

# Filter and format dataset
dataset <- dataset %>%
    filter(!grepl('^Other ', .$Vendor )) %>%    # Remove 'Other Vendors' from output
    filter(.$`Top Vendor Revenue` > 1)     # Workaround due to fact PBI does not allow variables to be passed to R

# Compute axis limites from dataset
xmin <- 0
xmax <- max(max(dataset$`Market Share`), 0.25)
ymin <- min(dataset$`Growth`)
ymax <- max(dataset$`Growth`)

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.