繁体   English   中英

Plotly in R - AB 对角线

[英]Plotly in R - Diagonal AB line

我需要一条穿过这个 plot 原点的对角线

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

library(plotly)

fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
fig

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

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")
  )))

结果

另请参阅此相关答案

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

请让我知道这是否是您的预期。

fit <- lm(Petal.Length ~ Sepal.Length, data = iris)

iris %>% 
  plot_ly(x = ~Sepal.Length) %>% 
  add_markers(y = ~Petal.Length) %>% 
  add_lines(x = ~Sepal.Length, y = fitted(fit))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM