conditional shading in plotly

a 夏天 提交于 2020-01-02 07:59:46

问题


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:

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?

回答1:


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:

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:



来源:https://stackoverflow.com/questions/38658766/conditional-shading-in-plotly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!