我有下面的代码生成散点图,我想更改图例的位置,使其仍然位于图的外部,但在中心或中间,我该怎么做?
f <- list(
family = "Courier New, monospace",
size = 18,
color = "#7f7f7f"
)
x <- list(
title = "Age of Buildings",
titlefont = f,
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE,
showgrid = TRUE
)
y <- list(
title = "Total Violations",
titlefont = f,
zeroline = FALSE,
showline = FALSE,
showticklabels = TRUE,
showgrid = TRUE
)
fig2 <- plot_ly(final, x=~agebuilding, y=~violationstotal, mode= "markers", color = ~INdexrehabless6, size = ~totalvalue)
fig2 <- fig2 %>% layout(xaxis = x, yaxis = y, legend=list(title=list(text='<b> Housing Conditions </b>'))) #chaging name legend
fig2
这是我得到的情节
有几种方法可以做到:
fig2 <- fig2 + layout(legend = list(x = 0.1, y = 0.9)) #puts it on the plot, mess with x and y numbers
fig2 <- fig2 + layout(legend = list(orientation = 'h')) #puts it on the below the plot
请参阅此以获取更多信息:https://plotly.com/r/legend/
基本上,您只需要对您的代码执行此操作:
fig2 <- fig2 %>% layout(xaxis = x, yaxis = y, legend=list(title = list(text='<b> Housing Conditions </b>', orientation = 'h')))
对于具有垂直方向的默认图例,其位置对应于
layout(legend = list(orientation = "v", y = 1, x = 1))
如果要将其放在y方向的底部,请使用
layout(legend = list(orientation = "v", y = 0, x = 1))
并且如果要使其在y方向上居中,请使用
layout(legend = list(orientation = "v", y = .5, x = 1))
如果是水平方向,则默认位置是
layout(legend = list(orientation = "h", y = -.1, x = 0))
并将图例放在图的左下角。