Skip to content

Adding vertical line to heatmap in 4.0+ #712

@robertmitchellv

Description

@robertmitchellv

I would like to add a vertical line with add_trace or add_lines in 4.0+ but it always represents it in 3D the different ways that I've tried it. Here is an example:

# The packages I'm using
library(tidyverse)
library(plotly) # 4.35

# This is similar to the data I'm using
year <- c(
  1900, 1901, 1902, 1903, 1904, 1905,
  1900, 1901, 1902, 1903, 1904, 1905,
  1900, 1901, 1902, 1903, 1904, 1905,
  1900, 1901, 1902, 1903, 1904, 1905,
  1900, 1901, 1902, 1903, 1904, 1905,
  1900, 1901, 1902, 1903, 1904, 1905)

state <- c(
  "California", "California", "California", "California", "California", "California",
  "Portland", "Portland", "Portland", "Portland", "Portland", "Portland",
  "Washington", "Washington", "Washington", "Washington", "Washington", "Washington",
  "Nevada", "Nevada", "Nevada", "Nevada", "Nevada", "Nevada",
  "Arizona", "Arizona", "Arizona", "Arizona", "Arizona", "Arizona", 
  "Idaho", "Idaho", "Idaho", "Idaho", "Idaho", "Idaho")

totals <- c(
  10, 15, 20, 25, 30, 5,
  8, 12, 40, 35, 39, 4,
  0, 0, 15, 19, 25, 2,
  1, 4, 8, 18, 22, 8,
  2, 2, 9, 14, 30, 7,
  0, 0, 0, 0, 10, 3)

disease <- data_frame(year, state, totals)

# I'm using this to have more control over the colors
range <- c(0.000, 0.500, 1.000)
hex <- c("#ffffff", "#E71B23", "#910007")
disease_color <- data.frame(range, hex)

plot_ly(data = disease, x = ~year, y = ~state, z = ~totals, type = "heatmap", 
        text = ~paste("Year: ", year, "<br>State: ", state,
                      "<br>Total Cases: ", totals),
        hoverinfo = "text", zmin = 0, zmax = 30, 
        colorscale = disease_color, showscale = F) %>%
  # this is where I'm stuck--trying to draw a vertical line for 1904
  add_trace(x = 1904, xend = 1904, y = ~min(state), yend = ~max(state), 
            mode = "lines") %>%
  layout(xaxis = list(title = ""),
         yaxis = list(title = ""))

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions