Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional shading in plotly

Tags:

r

plotly

I'm plotting two step functions using the R interface to plotly and shading the space between them, like so:

df <- structure(list(datetime = structure(c(1469869200, 1469871000, 
1469872800, 1469874600, 1469876400, 1469878200, 1469880000, 1469881800, 
1469883600, 1469885400, 1469887200, 1469889000, 1469890800, 1469892600, 
1469894400, 1469896200, 1469898000, 1469899800, 1469901600, 1469903400, 
1469905200, 1469907000, 1469908800, 1469910600), tzone = "Europe/Berlin", class = c("POSIXct", 
"POSIXt")), ya = c(0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 5, 
5, 5, 7, 7, 7, 7, 5, 2, 0, 0), yb = c(0, 0, 1, 2, 3, 2, 2, 2, 
2, 2, 1, 1, 2, 3, 4, 6, 8, 8, 8, 8, 8, 4, 1, 0)), class = "data.frame", row.names = c(NA, 
-24L), .Names = c("datetime", "ya", "yb"))

df %>%
    plot_ly(x=datetime, y=ya, line = list(shape = "hv")) %>%
    add_trace(x=datetime, y=yb, line = list(shape = "hv"), fill = "tonexty")

which gives me the following picture:

enter image description here

Is there any way I can:

  • change the color of the shading independent of the color of the line?
  • make the color of the shading be a function of whether the difference between the two lines is positive or negative?
like image 667
RoyalTS Avatar asked Jul 26 '26 08:07

RoyalTS


1 Answers

I manage to build it in ggplot2 and then converting to plotly with ggplotly().

By seperately creating the lines and the rectangles in between you can add the conditional coloring.

code:

ggplot(df) + 
  geom_step(aes(x = datetime, y = ya), colour = "red") +  # Create the ya line
  geom_step(aes(x = datetime, y = yb), colour = "blue") + # Create the yb line
  geom_rect(aes(xmin = datetime,                # Create rectangles strarting at every datetime
                xmax = datetime + minutes(30),  # It should reach untill the next datetime
                ymin = ifelse(ya > yb, yb, ya), # Lower bound is the lowest of ya and yb
                ymax = ifelse(ya > yb, ya, yb), # Upper bound is highest of ya and yb
                fill = ya > yb))                # Colour the rectangles based on condition

ggplotly()

Result:

enter image description here

By using the text aesthetic and the tooltip option in the ggplotly call, you can tweak what shows up in the hovers in the final plotly plot. For example this code:

ggplot(df, aes(x = datetime)) + 
  geom_step(aes(y = ya, text = paste("ya: ",ya,", yb: ",yb, sep = ""), group = 1), colour = "red")  +
  geom_step(aes(y = yb, text = paste("ya: ",ya,", yb: ",yb, sep = ""), group = 1), colour = "blue") +
  geom_rect(aes(xmin = datetime,
                xmax = datetime + minutes(30),
                ymin = ifelse(ya > yb, yb, ya),
                ymax = ifelse(ya > yb, ya, yb),
                fill = ya > yb,
                text = paste("ya: ",ya,", yb: ",yb, sep = "")))

ggplotly(tooltip = c("x","text"))

Would you give you tooltips displaying the time and both the ya and yb values like so: enter image description here

like image 82
Marijn Stevering Avatar answered Jul 28 '26 00:07

Marijn Stevering