Как сделать так, чтобы заголовок диаграммы проходил по нескольким строкам и выравнивался по левому краю на графике? - PullRequest
0 голосов
/ 30 января 2019

Я создаю трехмерную диаграмму рассеяния на графике и хочу иметь заголовок длиной в несколько предложений (для описания фигуры) с выравниванием по левому краю, не мешая самой фигуре и не являющийсяотрезать по площади участка.

Приведенный ниже код включает в себя ответ с изменением стека для сюжетов с выравниванием по левому краю, но это не помогает, если у вас длинный заголовок, который должен превышать 3-4 строки.

Использование \ n переводит текст заголовка во вторую строку, но он все равно обрезается по области графика и не выравнивается по левому краю.

library(plotly)

metric1 <- c(116,230,120,200,210,188,130,99,101,120)
metric2 <- c(79,109,120,95,130,98,118,130,140,88)
metric3 <- c(30,28,42,22,6,2,17,43,20,28)
class <- c(3,4,2,1,1,4,4,3,2,3)
df <- data.frame(metric1,metric2,metric3,class)

my_colors=c("red", "blue", "green", "#000000")[df$class]

p <- plot_ly(df, 
    x = ~metric1, 
    y = ~metric2, 
    z = ~metric3, text = class, type = "scatter3d", 
    mode = "markers", marker = list(color = my_colors)) %>%

    add_annotations(
    x=0, y=1.15, 
    text="Figure: The title of the figure will explain what information can be gleaned from the figure. Then the next sentence, which is still in this title, will elaborate on implications from the results. I want to be able to extend this as needed.", 
    font=list(size=17)
    ) %>% 

    layout(title = FALSE,
      scene = list(xaxis = list(title = 'metric 1', range = c(0,300)),
                 yaxis = list(title = 'metric 2', range = c(0,150)),
                 zaxis = list(title = 'metric 3', range = c(0,100))), showlegend = FALSE)

p

Вывод, который я получаю, показывает только конец заголовка и обрезает его:

enter image description here

Спасибо за любую помощь,

1 Ответ

0 голосов
/ 31 января 2019

Я думаю, с помощью align = "left" и /n даст вам то, что вы хотите:

metric1 <- c(116,230,120,200,210,188,130,99,101,120)
metric2 <- c(79,109,120,95,130,98,118,130,140,88)
metric3 <- c(30,28,42,22,6,2,17,43,20,28)
class <- c(3,4,2,1,1,4,4,3,2,3)
df <- data.frame(metric1,metric2,metric3,class)

my_colors=c("red", "blue", "green", "#000000")[df$class]

p <- plot_ly(df, 
             x = ~metric1, 
             y = ~metric2, 
             z = ~metric3, text = class, type = "scatter3d", 
             mode = "markers", marker = list(color = my_colors)) %>%

  add_annotations(
    x=0.4, y=0.9,
    align = "left",
   # text="Figure: The title of the figure will explain what information can be gleaned from the figure. Then the next sentence, which is still in this title, will elaborate on implications from the results. I want to be able to extend this as needed.", 
   text=paste("Figure: The title of the figure will explain what information can be gleaned from the figure",  "Then the next sentence, which is still in this title", "I want to be able to extend this as needed.", sep="\n") ,
   font=list(size=17)
  ) %>% 

  layout(title = FALSE,
         scene = list(xaxis = list(title = 'metric 1', range = c(0,300)),
                      yaxis = list(title = 'metric 2', range = c(0,150)),
                      zaxis = list(title = 'metric 3', range = c(0,100))), showlegend = FALSE)

p
...