R 中的情节 - 对角线 AB 线

Bra*_*rad 1 plot r plotly r-plotly abline

我需要一条穿过该图原点的对角线

类似于 ggplot2 的东西geom_abline(intercept = 0 , slope = 1) ,但是对于 R 中的plotly

library(plotly)

fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
fig
Run Code Online (Sandbox Code Playgroud)

ism*_*gal 5

可以使用线条形状来实现此目的:

library(plotly)

fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
fig %>%
  layout(shapes = list(list(
    type = "line", 
    x0 = 0, 
    x1 = ~max(Sepal.Length, Petal.Length), 
    xref = "x",
    y0 = 0, 
    y1 = ~max(Sepal.Length, Petal.Length),
    yref = "y",
    line = list(color = "black")
  )))
Run Code Online (Sandbox Code Playgroud)

结果

另请参阅此相关答案

顺便提一句。通过xref = "paper"我们不需要指定线的起点和终点,但是线不再与轴对齐。