我有以下代码生成散点图,并且我想更改图例的位置,以便使图例仍在图的外部,但在中心或中间,怎么办?
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
这是我得到的情节
答案 0 :(得分:1)
有几种方法可以做到这一点:
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')))
答案 1 :(得分:1)
对于具有垂直方向的默认图例,其位置对应于
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))
,然后将图例放在情节下方的左下角。