等同于 plotly 中的 abline
Equivalent of abline in plotly
我想在 plotly
图表中添加一条线(不仅仅是垂直线或水平线)
library(plotly)
d = data.frame(x=1:10, y=1:10)
plot_ly(d, x = ~x, y = ~y, type='scatter')
说我想要 (D) y = 2x
作为一条线 有没有办法让我在 d
的另一列中自己生成数据而不用自己绘制
至少有两种模拟方式abline
。
- 添加另一条带有起点和终点的轨迹
- 用起点和终点
向layout
添加一个line
形状
缺点是需要计算每个跟踪的 min
和 max
(Plotly
无论如何都需要这样做,但它会使代码膨胀)。
另一种方法是从一个极端到另一个极端画一条线,然后手动设置 xaxis
和 yaxis
range
。
在下面的例子中,红线是添加的轨迹,而黑线是通过shape
line
.
添加的
library('plotly')
#a function to calculate your abline
p_abline <- function(x, a=2, b=-5){
y <- a * x + b
return(y)
}
#create an empty plot
p <- plot_ly()
#list with all your x and y values
x <- list(c(1,2,3), c(2,3,6), (1:5))
y <- list(c(1,2,3), c(0,5,10), sample(-10:10, 5))
#loop through the x/y values and determine min/max
for (i in 1:length(x)) {
p<-add_trace(p, y=y[[i]], x=x[[i]] , type="scatter", mode="markers+lines")
if (i == 1 || max(x[[i]]) > max_x) {
max_x = max(x[[i]])
}
if (i == 1 || max(y[[i]]) > max_y) {
max_y = max(y[[i]])
}
if (i == 1 || min(x[[i]]) < min_x) {
min_x = min(x[[i]])
}
if (i == 1 || min(y[[i]]) < min_y) {
min_y = min(y[[i]])
}
}
#add a trace to simulate abline
p<-add_trace(p, x=c(min_x, max_x), y=c(p_abline(min_x), p_abline(max_x)) , type="scatter", mode="lines", name='abline')
#add a shape (line) to simulate abline
p <- p %>% layout(
shapes=list(type='line', x0=min_x, x1=max_x, y0=p_abline(min_x, -1, 3), y1=p_abline(max_x, -1, 3))
)
p
library(plotly)
d = data.frame(x=1:10, y=1:10)
plot_ly(d, x = ~x, y = ~y, type='scatter') %>%
add_trace(d, x = ~x, y = ~x * 2, type = "scatter")
事实证明,您可以在 add_trace 函数调用中直接进行数学运算,其中您说的是 y 应该是什么。因此,您可以在参数中直接将该列乘以二。
我想在 plotly
图表中添加一条线(不仅仅是垂直线或水平线)
library(plotly)
d = data.frame(x=1:10, y=1:10)
plot_ly(d, x = ~x, y = ~y, type='scatter')
说我想要 (D) y = 2x
作为一条线 有没有办法让我在 d
至少有两种模拟方式abline
。
- 添加另一条带有起点和终点的轨迹
- 用起点和终点 向
layout
添加一个line
形状
缺点是需要计算每个跟踪的 min
和 max
(Plotly
无论如何都需要这样做,但它会使代码膨胀)。
另一种方法是从一个极端到另一个极端画一条线,然后手动设置 xaxis
和 yaxis
range
。
在下面的例子中,红线是添加的轨迹,而黑线是通过shape
line
.
library('plotly')
#a function to calculate your abline
p_abline <- function(x, a=2, b=-5){
y <- a * x + b
return(y)
}
#create an empty plot
p <- plot_ly()
#list with all your x and y values
x <- list(c(1,2,3), c(2,3,6), (1:5))
y <- list(c(1,2,3), c(0,5,10), sample(-10:10, 5))
#loop through the x/y values and determine min/max
for (i in 1:length(x)) {
p<-add_trace(p, y=y[[i]], x=x[[i]] , type="scatter", mode="markers+lines")
if (i == 1 || max(x[[i]]) > max_x) {
max_x = max(x[[i]])
}
if (i == 1 || max(y[[i]]) > max_y) {
max_y = max(y[[i]])
}
if (i == 1 || min(x[[i]]) < min_x) {
min_x = min(x[[i]])
}
if (i == 1 || min(y[[i]]) < min_y) {
min_y = min(y[[i]])
}
}
#add a trace to simulate abline
p<-add_trace(p, x=c(min_x, max_x), y=c(p_abline(min_x), p_abline(max_x)) , type="scatter", mode="lines", name='abline')
#add a shape (line) to simulate abline
p <- p %>% layout(
shapes=list(type='line', x0=min_x, x1=max_x, y0=p_abline(min_x, -1, 3), y1=p_abline(max_x, -1, 3))
)
p
library(plotly)
d = data.frame(x=1:10, y=1:10)
plot_ly(d, x = ~x, y = ~y, type='scatter') %>%
add_trace(d, x = ~x, y = ~x * 2, type = "scatter")
事实证明,您可以在 add_trace 函数调用中直接进行数学运算,其中您说的是 y 应该是什么。因此,您可以在参数中直接将该列乘以二。