cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
SebastianLM Regular Visitor
Regular Visitor

R scripting dataset componet

Hello team!

 

I'm trying to plot a line chart using ggplot, and conditionally trying to assign the dimension level I want to see it. Bellow I you can find the code and in the attached images I try to describe my problem. Any ways here I go...

 

If I select "Category" in the slicer in blue and I don´t have "Producto Pharma" or any other level under "Category" added to the dataset of the R visual, it works fine, but if I add any of those, the line chart brokes. It happens the same if I select "Producto Pharma" and I have "Marca Pharma" because "Marca Pharma" is one level under "Producto Pharma" and "Producto Pharma" is one level under "Categoria". 

 

 I hope I've been clear and you can give me a hand to dynamically add the visual level dimension to that ggplot. Any additional information you may need please let me know. This is kinda urgent.

 

Thanks in advanced.

 

Here the code:

 

library(ggplot2)
library(grid)
library(scales)
library(ggrepel)

dataset$DATE <- as.Date(dataset$DATE, format="%Y-%m-%d")

fact = dataset$FACT
viz = dataset$ALLFILTRARM

if (viz == "CATEGORY") {
p <- ggplot(data = dataset , mapping = aes(x = DATE, group = interaction(CATEGORIA,MERCADO), color = CATEGORIA))
} else {
if (viz == "FABRICANTE PHARMA") {
p <- ggplot(data = dataset , mapping = aes(x = DATE, group = interaction(FABRICANTE,MERCADO), color = FABRICANTE))
} else {
if (viz == "MARCA PHARMA") {
p <- ggplot(data = dataset , mapping = aes(x = DATE, group = interaction(MARCA,MERCADO), color = MARCA))
}
}}

p + facet_wrap(~ MERCADO, nrow=1)+
scale_x_date(date_breaks = "1 month", date_labels = "%B %Y") +
xlab(" ")+
ylab(" ") +

geom_path(aes(y = Vts), size = 0.8) +

geom_point(aes(y = Vts), size = 1) +

geom_text(aes(label = format(Vts, digits = 0), y = Vts),hjust=0.5, vjust=-0.5,color="black" ,size = 4.5) +

ggtitle(paste(fact," X ",viz)) +

theme_minimal() +

theme(legend.title=element_blank(), plot.title = element_text(margin=margin(0,0,30,0), colour = "#00264d", hjust = 0.5,size=15, face="bold", vjust=1, lineheight=0.6), legend.position="bottom", legend.box = "horizontal", axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), panel.grid.major.x = element_blank(), panel.grid.minor = element_blank(), legend.spacing.x = unit(0.1, 'cm'))R1.PNG

 

R2.PNG

 

1 ACCEPTED SOLUTION

Accepted Solutions
SebastianLM Regular Visitor
Regular Visitor

Re: R scripting dataset componet

Hey team,  I created common Power BI line charts, one per dimension level and I played with the visibility of these objects with some visual bookmarks.

 

Thanks all!

3 REPLIES 3
Community Support Team
Community Support Team

Re: R scripting dataset componet

Hi SebastianLM,

 

You added column FABRICANTE to the R visual, right? Maybe this line in your code has been affected, delete it and try again:

p <- ggplot(data = dataset , mapping = aes(x = DATE, group = interaction(FABRICANTE,MERCADO), color = FABRICANTE))
}

Regards,

Jimmy Tao

SebastianLM Regular Visitor
Regular Visitor

Re: R scripting dataset componet

Hello @v-yuta-msft and thanks for your response,

 

The code didn't change and I wouldn't like to delete that line (I put it there intentionally) because based on the blue slicer selection I'd like to plot a different chart. I'm storing the slicer selection in "viz = dataset$ALLFILTRARM" (line 7). Sorry if I misunderstand you or if I wasn't clear since the OP.

SebastianLM Regular Visitor
Regular Visitor

Re: R scripting dataset componet

Hey team,  I created common Power BI line charts, one per dimension level and I played with the visibility of these objects with some visual bookmarks.

 

Thanks all!