r - 如何改变图例的位置

标签 r plotly r-plotly

我有以下代码生成散点图,我想更改图例的位置,以便它仍然位于图之外,但位于中心或中间,我该怎么做?

 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

这是我得到的 plotly

enter image description here

最佳答案

对于垂直方向的图例

  • 默认定位对应于

    布局(图例 = list(方向 = "v", y = 1, x = 1))

    并将图例放在绘图的右上角

  • 将其放在 y 方向底部使用

    布局(图例 = list(方向 = "v", y = 0, x = 1))

  • 使其在y 方向居中使用

    布局(图例 = list(方向 = "v", y = .5, x = 1))

如果是水平方向

  • 默认定位是

    布局(图例 = list(方向 = "h", y = -.1, x = 0))

    并将图例放在图下方的左下角中。

  • 将其放在 y 方向顶部使用

    布局(图例 = list(方向 = "v", y = 1.1, x = 0))

关于r - 如何改变图例的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61236977/

相关文章:

python - 开始使用plotly时导入报错

将曲面图中的线条删除到 x、y 和 z 平面

python - 如何使用下载的bigml模型进行本地预测?

减少 ggplot 箱线图中离散轴末端的空间

python - 在绘图服务器上存储静态图像

python - 背景图像来自 Google Drive 中的 plotly

r - 粗体绘图 (R) 饼图标签

r - 如何将子图应用于具有辅助 y 轴的图列表

r - 在 cowplot : How to insert gap between main panel and marginal plots 中使用 axis_canvas 的边缘图

r - 通过连接列连接两个数据框