2017-03-16 14 views
0

Rで、ある因子でグループ化された指標の線や棒を表示するインタラクティブなグラフを表示できるようにしたい。因子のレベル(グループ)の1つ以上を消し、x軸とy軸をそれに適応させ、この選択に応答する。Rインタラクティブグラフ - 因子レベルの要素を削除して軸を適応させる

例:

df <- data.frame(factor1 = c(rep("a", 3), rep("b", 3), rep("c", 3)), 
      xAxisVar = c(1:7, 5, 9), 
      yAxisVar = c(1:7, 5, 25)) 

ggplot(df, aes(xAxisVar, yAxisVar, group = factor1, color = factor1)) + 
    geom_line() + 
    geom_point() 

enter image description here

このグラフのy軸が原因因子レベル "C" 1 "大" の観察25に到達するように拡張されます。私は "c"を押すか、それをフィルタリングし、y軸がそれに応答するようにしたい、プロットを6に近づけるように再描画したい。

私はのggplotlyを試していますが、自動的にグループ "c"を消すことができますが、そのことを考慮してプロットの再描画はできません。提案?

答えて

2

あなたはggplotlyを述べたので、あなたはplotlyに開かれている場合、あなたは試みることができる:

library(plotly) 
library(reshape2) 

#from long to wide 
df_wide <- dcast(df, xAxisVar ~ factor1, value.var="yAxisVar") 

plot_ly(df_wide, x = ~xAxisVar, y = ~a, name='a', type='scatter', mode='lines+markers') %>% 
    add_trace(y = ~b, name = 'b', type='scatter', mode='lines+markers') %>% 
    add_trace(y = ~c, name = 'c', type='scatter', mode='lines+markers', connectgaps = TRUE) %>% 
layout(
    updatemenus = list(
    list(
     type = "buttons", 
     x = -0.1, 
     y = 0.7, 
     label = 'Category', 
     buttons = list(
     list(method = "restyle", 
      args = list('visible', c(TRUE, FALSE, FALSE)), 
      label = "a"), 
     list(method = "restyle", 
      args = list('visible', c(FALSE, TRUE, FALSE)), 
      label = "b"), 
     list(method = "restyle", 
      args = list('visible', c(FALSE, FALSE, TRUE)), 
      label = "c") 
    ) 
    ))) 
+0

は、おかげで非常に良い感じです。 –

関連する問題