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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
prateekraina
Memorable Member
Memorable Member

Need help in R Visual

Hi Everyone,

 

I am in process of creatiing a R Visual in which i need to change color of line above threshold.
So far, i have written below script to genrate the visual uploaded below. Can someone help me to get my desired result i.e If my threshold is breahed/surpassed, color of line should change.

If this can be achived using traditional Power BI visuals then that suggestion is also welcome.
Here is my code and visual created so far.

plot(dataset$Temperature,type="l",lwd=2,
xaxt="n",col="blue",
xlab="Date",ylab="Temperature",
main="Demo Chart")
axis(1,at=1:length(dataset$Date),labels=dataset$Date)
abline(h=dataset$TargetTemperature,col="red")

Solution1.PNGSolution2.PNG

Edit 1: Sorry for tagging you guys. Kindly help if anybody knows.


@Greg_Deckler

@MarcelBeug

@Vvelarde

@Phil_Seamark

@MFelix

@v-haibl-msft
@v-huizhn-msft
@Seth_C_Bauer

1 ACCEPTED SOLUTION

Try below R code. Multiple colours works for points i.e. col = ifelse(temperature >15,"blue","red") etc  but does not work for line charts in base R plot.

 

plot(dataset$Temperature,type = "l",col = "blue")
abline(h=10,col = "red")
clip(0,100,0,10)
lines(dataset$Temperature, col='green', type='l')

 

View solution in original post

10 REPLIES 10
MFelix
Super User
Super User

Hi @prateekraina,

 

I was looking at your data and I believe this is possible doing to measures and adding the visuals on a line chart at a specific order.

 

Above = SWITCH(TRUE(),MAX(Temperatures[Temperature])>10,MAX(Temperatures[Temperature]),"")

Below = SWITCH(TRUE(),MAX(Temperatures[Temperature])<=10,MAX(Temperatures[Temperature]),"")

I supposed the threshold was 10 but you can make this a specific value as I did or a variable according to a slicer.

 

Then select the Line chart and add Temperature, Below, Above, making in this order will make you achieve the lines on top of each others and when you have changes in the temperature that go more than the threshold you have a line, also be aware the you need to have the X-axis to categorical in order to have the null values vanish from your lines grey lines.

 

Sorry for refering this again but be aware fo the way you add the measures in your graph since if you had the temperature in the end it will be over the other two line and you only see that line, this line is just to join the points were the graph transfers from the below to above threshold and vice-versa.

 

Line_Colours.png

 

Regards,

MFelix

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi @MFelix,

 

I understood. Your data set will be little diff so only difference is there.
But my problem is that these grey lines which are connecting Red with Green and Vice versa, can we somehow keep them as same color till they reach threshold line and then they change?

I know not possible in Power BI but still taking chance by asking.


Try below R code. Multiple colours works for points i.e. col = ifelse(temperature >15,"blue","red") etc  but does not work for line charts in base R plot.

 

plot(dataset$Temperature,type = "l",col = "blue")
abline(h=10,col = "red")
clip(0,100,0,10)
lines(dataset$Temperature, col='green', type='l')

 

Hi @lalthan,

 

Thank you for the snippet.

I am almost there, just one question. Can you please let me know how do i plot it against Date as shown in my problem set snapshot.
Currently temperature is getting plotted against Index (1 - 10). Refer below screenshot:
Solution.PNG

 

Regards,

Prateek Raina

The X-axis was already defined in the later part of your R code. However, try this

 

plot(x = dataset$Date, y = dataset$Temperature,type = "l",col = "blue")
abline(h=10,col = "red")
clip(0,100,0,10)
lines(dataset$Temperature, col='green', type='l')

Hi @lalthan,

 

I tried this one but am getting odd results. Refer below screenshot:
Solution1.PNG

After adding below line, it happens

plot(x = dataset$Date, y = dataset$Temperature,type = "l",col = "blue")

Any workaround?

Got it working by using below line:

axis(1,at=1:length(dataset$Date),labels=dataset$Date)


Thank you very much @lalthan and @MFelix. Appreciate your time.

Regards,

Prateek Raina

Hi @MFelix,

 

Thank you for the prompt response.
I followed the steps which you mentioned. I appreciate your approach but i am not getting the same result as shown in your screenshot.
Can you pls share your pbix where you tried. 

Thanks !!!

Hi @prateekraina,

 

In attach PBIX file.

 

https://we.tl/pA0xSOQf4i

 

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hi @MFelix,

 

Thank you very much for your effort.

 

Regards,

Prateek Raina

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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