Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Plotly rotation of text in add_text

Tags:

r

plotly

I can't seem to get the text to rotate in Plotly, in a scatter plot, using add_text().

I'm just trying to get the same result that the angle argument yields in ggplot. In plotly, the output needs to have the hovertext if that's of consequence.

Example -

library(dplyr)
library(plotly)

data <- data.frame(
  x = 1:10,
  y = runif(10,0,10),
  lab = LETTERS[1:10]
)

# base output needed in ggplot
p <- data %>%
  ggplot(aes(x,y)) +
  geom_text(aes(label = lab, angle = 90))

# doesn't respect angle arg - not that I'm looking to use ggplotly
ggplotly(p)

# plotly version
plot_ly(data) %>%
  add_text(
    x = ~x,
    y = ~y,
    text = ~lab,
    hovertext = ~paste0("Label is ", lab),
    # things I've tried (generally one at a time..)
    textfont = list(angle = 90, textangle = 90, orientation = 90, rotate = 90)
  )

I'm sure I'm missing something obvious, but I can't track it down.. Help pls!

like image 395
abarnsey Avatar asked Jan 20 '26 15:01

abarnsey


1 Answers

It appears the solution is to use add_annotations() rather than add_text(). A textangle arg is then accpeted.

Edit - turns out you need two traces - annotations to achieve the text rotation, then markers for the hovertext. Setting opacity = 0 for the markers seems OK.

like image 180
abarnsey Avatar answered Jan 22 '26 05:01

abarnsey