分割线图R的背景
我想将线图分为两个区域(左侧和右侧)。我正在规划相同的历史价值和预测价值,我梦想着拥有不同的历史和预测价值背景。我更喜欢使用Plotly库 有人知道这是否可能以及如何做到吗分割线图R的背景,r,shiny,plotly,dt,R,Shiny,Plotly,Dt,我想将线图分为两个区域(左侧和右侧)。我正在规划相同的历史价值和预测价值,我梦想着拥有不同的历史和预测价值背景。我更喜欢使用Plotly库 有人知道这是否可能以及如何做到吗 您可以将矩形添加到您的布局中,该布局的尺寸与您的测量和预测数据相同。创建两个单独的记录道,一个用于实际数据,一个用于预测数据,或者使用合并的记录道 两条记录道 set.seed(42) x <- sort(rnorm(25)) y <- x + rnorm(5) predict(lm(y ~ x)) new
您可以将
矩形
添加到您的布局
中,该布局的尺寸与您的测量和预测数据相同。创建两个单独的记录道,一个用于实际数据,一个用于预测数据,或者使用合并的记录道
两条记录道
set.seed(42)
x <- sort(rnorm(25))
y <- x + rnorm(5)
predict(lm(y ~ x))
new_x <- seq(max(x) + 0.1, 5, 1)
new <- data.frame(x = new_x)
pred <- predict(lm(y ~ x), new, se.fit = TRUE)
new_y <- unname(pred$fit)
p <- plot_ly()
p <- add_trace(p, x = x, y = y, name = 'real', type = 'scatter', mode = 'scatter+lines', line = list(shape = 'spline'))
p <- add_trace(p, x = new_x, y = new_y, name = 'predicted', type = 'scatter', mode = 'scatter+lines', line = list(shape = 'spline'))
p <- layout(p,
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = min(x), x1 = max(x), xref = "x",
y0 = min(y), y1 = max(new_y), yref = "y"),
list(type = "rect",
fillcolor = "red", line = list(color = "blue"), opacity = 0.2,
x0 = max(x), x1 = max(new_x), xref = "x",
y0 = min(y), y1 = max(new_y), yref = "y")))
p
set.seed(42)
x
set.seed(42)
x <- sort(rnorm(25))
y <- x + rnorm(5)
predict(lm(y ~ x))
new_x <- seq(max(x) + 0.1, 5, 1)
new <- data.frame(x = new_x)
pred <- predict(lm(y ~ x), new, se.fit = TRUE)
new_y <- unname(pred$fit)
all_x = c(x, new_x)
all_y = c(y, new_y)
p <- plot_ly()
p <- add_trace(p, x = all_x, y = all_y, type = 'scatter', mode = 'scatter+lines', line=list(shape='spline'))
p <- layout(p,
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = min(x), x1 = max(x), xref = "x",
y0 = min(y), y1 = max(new_y), yref = "y"),
list(type = "rect",
fillcolor = "red", line = list(color = "blue"), opacity = 0.2,
x0 = max(x), x1 = max(new_x), xref = "x",
y0 = min(y), y1 = max(new_y), yref = "y")))
p