Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
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")
Edit 1: Sorry for tagging you guys. Kindly help if anybody knows.
@v-haibl-msft
@v-huizhn-msft
@Seth_C_Bauer
Solved! Go to 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')
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.
Regards,
MFelix
Regards
Miguel Félix
Proud to be a Super User!
Check out my blog: Power BI em PortuguêsHi @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:
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:
After adding below line, it happens
plot(x = dataset$Date, y = dataset$Temperature,type = "l",col = "blue")
Any workaround?
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.
Regards,
MFelix
Regards
Miguel Félix
Proud to be a Super User!
Check out my blog: Power BI em PortuguêsUser | Count |
---|---|
117 | |
74 | |
62 | |
50 | |
46 |
User | Count |
---|---|
174 | |
125 | |
60 | |
60 | |
57 |