【问题标题】:Plotly in R - Diagonal AB line在 R - 对角 AB 线上绘图
【发布时间】:2021-12-11 09:10:15
【问题描述】:

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

类似于ggplot2的geom_abline(intercept = 0 , slope = 1) 但是对于 R 中的情节而言

library(plotly)

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

【问题讨论】:

  • 不幸的是,这似乎是我能找到的最好的例子:stackoverflow.com/questions/41980772/… 使用 ggplotly 可能更容易一些,但我不能保证。
  • 如果您在 plotly 中寻找趋势线(这是 abline 所扮演的角色,Q & A 可能会有所帮助。
  • 可能作弊,但是:library(plotly); library(ggplot); fig &lt;- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) + geom_point() + geom_abline(); ggplotly(fig)
  • 你可以试试add_segments。试试p &lt;- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter") %&gt;% add_segments(x=4, y=4, xend = 8, yend = 8) p

标签: r plot plotly diagonal abline


【解决方案1】:

如果这是您所期待的,请告诉我。

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

【讨论】:

    猜你喜欢
    • 2013-08-06
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2015-11-20
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多