2016-04-15 63 views
0

我有一個閃亮的反應ggvis scatterplot(layer_points)。 現在我想在圖中添加水平線和垂直線,以類似於x/y軸的中值。shiny + ggvis:如何將一條線(中值)添加到scatterplot?

我知道如何計算它,但不知道如何顯示它在同一個情節。 到目前爲止我的代碼:

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

感謝很多幫助的。

+0

我認爲[這](http://stackoverflow.com/questions/31666210/how-can-i-add-a-horizo​​ntal-line-in-ggvis)將回答你的問題,無論是答案或評論。 – aosmith

+0

它似乎是一個好主意,使用mutate,然後渲染它的情節。但我無法讓它工作。問題是每次輸入改變時需要計算中位數。如上所示,我知道如何計算即使在反應中的中位數。但我不知道將它添加到情節中。當它在情節制作完成後得到計算後,我如何在情節中獲得它?可能很簡單,但我無法讓它工作。 –

回答

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的正確方向。