Ntgllr
Ntgllr

Reputation: 85

Additional Labels in Plotly Sankey Diagram

I created a Sankey Diagram using Plotly in R and want to add vertical labels to the far left and right of the plot. Is this possible? I found the possibility of Annotations in Plotly but can't get it to work with the Diagram.

Code looks like this:

library(plotly)
fig <- plot_ly(
  type = "sankey",
  orientation = "h",
  
  node = list(
    label = c("Level 1", "Level 2", "Level 3",
              "Level 1", "Level 2", "Level 3", "Level 4", "Level 5"),
    color = c("rgba(34,139,34, 1)", "rgba(31, 119, 180, 1)", "rgba(255, 127, 14, 1)",
              "rgba(34,139,34, 1)", "rgba(31, 119, 180, 1)", "rgba(255, 127, 14, 1)", "rgba(148, 103, 189, 1)", "rgba(148, 103, 0, 1)"),
    pad = 20,
    thickness = 15,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  
  link = list(
    source = c(0,0,0,0,1,1,1,1,2,2,2,2),
    target = c(3,4,5,6,3,4,5,6,4,5,6,7),
    value =  c(482,52,1,1,20,127,19,1,1,1,3,1),
    color = c("rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)",
              "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)",
              "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)")
  )
)
fig <- fig %>% layout(
  title = "Title of the Plot",
  font = list(
    size = 10
  )
)

fig

Thanks for your help.

EDIT:

I tried using "add_annotations", but as soon as I shift the x-axis of the text lower than 0 (to move the text farther left) it vanishes:

%>% add_annotations(
  xref="paper",
  yref="paper",
  x=-0.1,
  y=0.5,
  text="Test",
  showarrow=FALSE
)

Upvotes: 1

Views: 1895

Answers (2)

norie
norie

Reputation: 9867

Not an answer, only a suggestion to slightly improve the code.

Instead of using add_annotations twice you can add both labels in one go.

add_annotations(
  x = c(1,0),
  y = 0.5,
  xshift = c(25, -25),
  text = c("Text 1", "Text 2"),
  font = list(
    size = 14
  ),
  showarrow = FALSE,
  textangle = c(90, -90)
)

Upvotes: 0

Ntgllr
Ntgllr

Reputation: 85

After some more tinkering I found the Solution myself:

I added some margin to the layout:

layout(
  title = "Name of the Plot",
  font = list(
    size = 10
  ),
  margin = list(
    r = 30,
    l = 30
  )

Then I used "add_annotations" to create the text and shifted it further to the right/left using "xshift":

add_annotations(
  xref = "paper",
  yref = "paper",
  x = 1,
  y = 0.5,
  xshift = 25,
  text = "Text I wanted to add",
  font = list(
    size = 14
  ),
  showarrow = FALSE,
  textangle = 90

The final code looks something like this:

fig <- plot_ly(
  type = "sankey",
  orientation = "h",
  node = list(
    label = c("Level 1", "Level 2", "Level 3",
              "Level 1", "Level 2", "Level 3", "Level 4", "Level 5"),
    color = c("rgba(34,139,34, 1)", "rgba(31, 119, 180, 1)", "rgba(255, 127, 14, 1)",
              "rgba(34,139,34, 1)", "rgba(31, 119, 180, 1)", "rgba(255, 127, 14, 1)", "rgba(148, 103, 189, 1)", "rgba(214, 39, 40, 1)"),
    pad = 20,
    thickness = 15,
    line = list(
      color = "black",
      width = 0.5
    )
  ),
  
  link = list(
    source = c(0,0,0,1,1,1,1,2,2,2,2,2),
    target = c(3,4,5,3,4,5,6,3,4,5,6,7),
    value =  c(614,27,4,12,35,6,1,1,1,2,5,1),
    color = c("rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)", "rgba(34,139,34,0.4)",
              "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)", "rgba(31, 119, 180, 0.4)",
              "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)", "rgba(255, 127, 14, 0.4)")
  )
) %>% layout(
  title = "Name of the plot",
  font = list(
    size = 10
  ),
  margin = list(
    r = 30,
    l = 30
  )
) %>% add_annotations(
  x = 1,
  y = 0.5,
  xshift = 25,
  text = "Text 1",
  font = list(
    size = 14
  ),
  showarrow = FALSE,
  textangle = 90
) %>% add_annotations(
  x = 0,
  y = 0.5,
  xshift = -25,
  text = "Text 2",
  font = list(
    size = 14
  ),
  showarrow = FALSE,
  textangle = -90
) %>% show()

Upvotes: 2

Related Questions