Draw a line with intercept and slope in ggvis

本小妞迷上赌 提交于 2019-12-12 11:55:25

问题


Just getting start with Shiny. I ask you: is it possible to add single lines characterized by intercept and slope to ggvis plot?

For example with ggplot2 using geom_abline.

The code that i'm interested to debat is in the server.R file located at https://github.com/wch/movies and refers to this example http://shiny.rstudio.com/gallery/movie-explorer.html.

This is a way to draw a line

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

but i'm interested to draw it OVER an existent ggvis plot, that of link above.

I should add some code here:

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

回答1:


I found the solution, i thank a dear friend for this:

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)

The coordinates refer to the usual example http://shiny.rstudio.com/gallery/movie-explorer.html.




回答2:


I'm interested in a solution that introspects the vis to get the domain.

Here's what I came up with (which also doesn't do that):

abline_data <- function (domain, intercept, slope) {
  data.frame(x = domain, y = domain * slope + intercept)
}

untick <- function (x) {
  # Hack to remove backticks from names
  stopifnot(all(sapply(x, is.name)))
  str_replace_all(as.character(x), "`", "")
}

layer_abline <- function (.vis, domain, intercept = 0, slope = 1) {
  df <- abline_data(domain, intercept, slope)
  names(df) <- with(.vis$cur_props, untick(c(x.update$value, y.update$value)))
  layer_paths(.vis, data = df)
}

cars %>% ggvis(x = ~speed, y = ~dist) %>% 
  layer_points() %>% 
  layer_abline(domain = c(0, 26))


来源:https://stackoverflow.com/questions/24584666/draw-a-line-with-intercept-and-slope-in-ggvis

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!