Reputation: 8454
How can I put the legend on top of the plotly chart instead of bottom?
library(plotly)
library(tidyr)
library(plyr)
data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))
fig <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1')
fig <- fig %>% add_trace(y = ~Tree2, name = 'Tree 2')
fig <- fig %>% add_trace(y = ~Tree3, name = 'Tree 3')
fig <- fig %>% add_trace(y = ~Tree4, name = 'Tree 4')
fig <- fig %>% add_trace(y = ~Tree5, name = 'Tree 5')
fig <- fig %>% layout(legend = list(orientation = 'h'))
fig
Upvotes: 2
Views: 133
Reputation: 41523
You could specify the x and y coordinates of the legend
in layout
like this:
library(plotly)
library(tidyr)
library(plyr)
fig <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1')
fig <- fig %>% add_trace(y = ~Tree2, name = 'Tree 2')
fig <- fig %>% add_trace(y = ~Tree3, name = 'Tree 3')
fig <- fig %>% add_trace(y = ~Tree4, name = 'Tree 4')
fig <- fig %>% add_trace(y = ~Tree5, name = 'Tree 5')
fig <- fig %>% layout(legend = list(orientation = 'h', x = 0, y = 1.1))
fig
Created on 2023-03-13 with reprex v2.0.2
Upvotes: 4