有光泽的+ ggvis:如何在散点图中添加一条线(中位数)?

时间:2016-04-15 13:54:58

标签: r scatter-plot ggvis shiny

我有一个闪亮的反应性ggvis散点图(layer_points)。 现在我想在图中添加一条水平线和一条垂直线,使其类似于x / y轴的中位数。

我知道如何计算它,但不知道如何在同一个地块中显示它。 我的代码到目前为止:

vis <- reactive({
  # Lables for axes
  xvar_name <- names(axis_vars)[axis_vars == input$xvar]
  yvar_name <- names(axis_vars)[axis_vars == input$yvar]

  xvar <- prop("x", as.symbol(input$xvar))
  yvar <- prop("y", as.symbol(input$yvar))

  gegevens %>%
    ggvis(x = xvar, y = yvar) %>%
    layer_points(size := 50, size.hover := 200,
                 fillOpacity := 0.2, fillOpacity.hover := 0.5,
                 stroke = ~bron, key := ~Project.ID) %>%
    add_tooltip(gegevens_tooltip, "hover") %>%
    add_axis("x", title = xvar_name, format='d', grid = FALSE) %>%
    add_axis("y", title = yvar_name, format='d', grid = FALSE) %>%
        add_legend("stroke", title = "Gegevens van:", values = c("A", "B")) %>%
    scale_numeric("x", trans = "log", expand=0) %>%
    scale_numeric("y", trans = "log", expand=0) %>%
    scale_nominal("stroke", domain = c("A", "B"),
                  range = c("blue", "#aaa")) %>%
    set_options(width = 600, height = 600)
})

vis %>% bind_shiny("plot1")

计算我使用的中位数:

    output$defects <- renderText ({
  d <- median(gegevens()$Total.Defects.Delivered) 
  paste("de mediaan voor totaal aantal Defects is:", d)
})

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

似乎我误解了你的例子,但我得到了它的工作,就在我发布后我不能。那么这就是解决方案:

vis <- reactive({
  # Lables for axes
  xvar_name <- names(axis_vars)[axis_vars == input$xvar]
  yvar_name <- names(axis_vars)[axis_vars == input$yvar]

  xvar <- prop("x", as.symbol(input$xvar))
  yvar <- prop("y", as.symbol(input$yvar))

  gegevens %>%
    ggvis(x = xvar, y = yvar) %>%
    layer_points(size := 50, size.hover := 200,
                 fillOpacity := 0.2, fillOpacity.hover := 0.5,
                 stroke = ~bron, key := ~Project.ID) %>%
    add_tooltip(gegevens_tooltip, "hover") %>%
    add_axis("x", title = xvar_name, format='d', grid = FALSE, properties = axis_props(labels = list(angle = 90, align = "left"))) %>%
    add_axis("y", title = yvar_name, format='d', grid = FALSE) %>%
        add_legend("stroke", title = "Gegevens van:", values = c("A", "B")) %>%
    scale_numeric("x", trans = "log", expand=0) %>%
    scale_numeric("y", trans = "log", expand=0) %>%
    scale_nominal("stroke", domain = c("A", "B"),
                  range = c("blue", "#aaa")) %>%
    set_options(width = 600, height = 600) %>%
    layer_paths(data = gegevens, x = median(gegevens()$kolomname.i.want.the.median.from)), y = yvar ) %>%
    layer_paths(data = gegevens, x = xvar, y = median(gegevens()$kolomname.i.want.the.median.from))
})
通过计算x和y的中位数,即使用户改变了原始输入,这也给了我一个十字架。当然我需要找出如何将“kolomname.i.want.the.median.from”作为x- /或y值。

但我现在知道如何排队,这就是问题所在。 所以,谢谢aosmith的正确方向。