2016-05-16 10 views
5

実際にどの点が表示されているかにかかわらず、プロットの色の目盛りとサイズの目盛りを維持しようとしています。光沢がプロットの要素を更新しないようにする

ggplotには、scale_color_gradient(limits=c(0,1))などの引数を使用してこれらの定数を設定できます。しかし、私はplot_lyで並列関数を見つけることができません。その他の理由で、ここではggplotly()を使用することはできません。

私はこれもeventReactive()で行うことができると信じていますが、使用方法の理解に問題があります。

最小の例ですが、プロットの色とサイズが変わり続けるところです。

library(dplyr) 
library(shiny) 
library(plotly) 

df <- as.data.frame(list("UserID"=c(1,1,1,1,2,2,2,2), 
          "Category"=c('A','A','B','B','A','A','B','B'), 
          "Rate"=c(2,3,5,6,8,6,7,1), 
          "x"=c(1,3,5,7,2,4,6,8), 
          "y"=c(1,3,5,7,2,4,6,8) 
        )) 

ui <- (fluidPage(
    sidebarLayout(
    sidebarPanel(
     selectInput("userInput","Select User", sort(unique(df$UserID)), 
        selected=1), 
     checkboxGroupInput("CategoryInput", "Select Category", sort(unique(df$Category)), 
         selected=c('A','B')) 
    ), 

    mainPanel(
     plotlyOutput("mainPlot")#, 
    ) 
) 
)) 

server <- function(input, output, session) { 

    # filter for both user and category 
    filteredFull <- reactive({ 
     df %>% 
     filter(
      UserID == input$userInput, 
      Category %in% input$CategoryInput 
     ) 
    }) 

    output$mainPlot <- renderPlotly({ 
     plot_ly(data=filteredFull(), x=x, y=y, 
         color=Rate, size=Rate, 
         mode='markers') 
    }) 
} 

shinyApp(ui, server) 

カラー/サイズのスケール/範囲を更新することなく、表示されるポイントのみを更新することはできますか?

答えて

5

Plotlyにもこれらのカラースケールの制限があります。それらはmarker属性の下にネストされます。色のスケールの最大値と最小値(数値)を設定して、どの点が実際に表示されているかに関わらずスケールを一定にすることができます。

plot_lyコマンドにmarker = list(cmin = 1, cmax = 8)を追加したばかりです。だから、dfをスキャンして最大値と最小値がどれかを判断する必要があります。以下

コード:

library(dplyr) 
library(shiny) 
library(plotly) 

df <- as.data.frame(list(
    "UserID" = c(1, 1, 1, 1, 2, 2, 2, 2), 
    "Category" = c('A', 'A', 'B', 'B', 'A', 'A', 'B', 'B'), 
    "Rate" = c(2, 3, 5, 6, 8, 6, 7, 1), 
    "x" = c(1, 3, 5, 7, 2, 4, 6, 8), 
    "y" = c(1, 3, 5, 7, 2, 4, 6, 8) 
)) 

ui <- (fluidPage(
    sidebarLayout(
    sidebarPanel(
     selectInput("userInput","Select User", sort(unique(df$UserID)), selected=1), 
     checkboxGroupInput("CategoryInput", "Select Category", sort(unique(df$Category)), selected=c('A','B')) 
    ), 

    mainPanel(
     plotlyOutput("mainPlot"), 
     plotlyOutput("mainPlotFixedSize") 
    ) 
) 
)) 

server <- function(input, output, session) { 

    # filter for both user and category 
    filteredFull <- reactive({ 
     df %>% 
     filter(
      UserID == input$userInput, 
      Category %in% input$CategoryInput 
     ) 
    }) 

    output$mainPlot <- renderPlotly({ 
    plot_ly(data=filteredFull(), x=x, y=y, 
     color=Rate, size=Rate, 
     mode='markers', marker = list(cmin = 1, cmax = 8)) 
    }) 

    output$mainPlotFixedSize <- renderPlotly({ 
    plot_ly(data=filteredFull(), x=x, y=y, 
     color=Rate, mode='markers', 
     marker = list(sizemode = "area", size = Rate*3400, cmin = 1, cmax = 8)) 
    }) 
} 

shinyApp(ui, server) 
+0

これは、カラースケールに最適です!ありがとうございました!サイズはどうですか?私はドキュメンテーションで何も見つけることができませんでした。 –

+1

少し遅れましたが、サイズを含めるために私の答えを編集しました。サイズの制御はずっと困難です。実際には簡単ですが、すべてのサイズ調整を手作業で行う必要があります。私は実験によってRate factor(最低3400 px面積)を選択しました。 –

+0

@Adam_G更新された回答が効果的かどうかを確認できますか? –

関連する問題