gpt4 book ai didi

r - 在ggvis中用截距和斜率画一条线

转载 作者:行者123 更新时间:2023-12-05 00:25:22 24 4
gpt4 key购买 nike

刚刚开始使用 Shiny。请问:是否可以在上添加以截距和斜率为特征的单线ggvis 阴谋?

例如使用 geom_abline 的 ggplot2。

我有兴趣讨论的代码在位于 https://github.com/wch/movies 的 server.R 文件中并引用此示例 http://shiny.rstudio.com/gallery/movie-explorer.html .

这是一种画线的方法

x_min <- 0
x_max <- 10

m <- 1
b <- 5

x <- c(x_min, x_max)
y <- m*x + b

df <- data.frame(x = x, y = y)

df %>% ggvis(x = ~x, y = ~y) %>% layer_lines()

但我有兴趣将它绘制在现有的 ggvis 图上,即上面的链接图。

我应该在这里添加一些代码:
movies %>%
ggvis(x = xvar, y = yvar) %>%
layer_points(size := 50, size.hover := 200,
fillOpacity := 0.2, fillOpacity.hover := 0.5,
stroke = ~has_oscar, key := ~ID) %>%
add_tooltip(movie_tooltip, "hover") %>%
add_axis("x", title = xvar_name) %>%
add_axis("y", title = yvar_name) %>%
add_legend("stroke", title = "Won Oscar", values = c("Yes", "No")) %>%
scale_nominal("stroke", domain = c("Yes", "No"),
range = c("orange", "#aaa")) %>%
set_options(width = 500, height = 500)
})

最佳答案

我找到了解决方案,我为此感谢一位亲爱的 friend :

data_line <- data.frame(
x_rng = c(0, 100),
y_rng = c(80, 200)
)

movies %>%
ggvis(x = xvar, y = yvar) %>%
layer_points(size := 50, size.hover := 200,
fillOpacity := 0.2, fillOpacity.hover := 0.5,
stroke = ~has_oscar, key := ~ID) %>%
### A couple of ways to display lines
layer_model_predictions(model = "lm", stroke := "red") %>%
layer_paths(x = ~x_rng, y = ~y_rng, stroke := "blue", data = data_line) %>%
###
add_tooltip(movie_tooltip, "hover") %>%
add_axis("x", title = xvar_name) %>%
add_axis("y", title = yvar_name) %>%
add_legend("stroke", title = "Won Oscar", values = c("Yes", "No")) %>%
scale_nominal("stroke", domain = c("Yes", "No"),
range = c("orange", "#aaa")) %>%
set_options(width = 500, height = 500)

坐标引用通常的例子 http://shiny.rstudio.com/gallery/movie-explorer.html .

关于r - 在ggvis中用截距和斜率画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24584666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com