R语言 将醒目的图例放在图表顶部

bfnvny8b  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(127)

我怎样才能把图例放在图表的顶部而不是底部呢?

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
mf98qq94

mf98qq941#

您可以在layout中指定legend的x和y坐标,如下所示:

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

创建于2023年3月13日,使用reprex v2.0.2

相关问题