2017-12-30 26 views
1

にモザイクプロットの塗りつぶしを変更するI以下の光沢のあるアプリを持っている:私は今、2番目のプロットは、対話したいと思いシャイニー

library(shiny) 
library(ggplot2) 
library(dplyr) 
library(networkD3) 
library(ggmosaic) 

#Loading data 
Category <- c("Bankpass", "Bankpass", "Bankpass", "Moving", "Moving") 
Subcategory <- c("Stolen", "Lost", "Login", "Address", "New contract") 
Weight <- c(10,20,13,40,20) 
Duration <- as.character(c(0.2,0.4,0.5,0.44,0.66)) 
Silence <- as.character(c(0.1,0.3,0.25,0.74,0.26)) 
df <- data.frame(Category, Subcategory, Weight, Duration, Silence) 

ui <- fluidPage(


    tags$div(class="header", 
     selectInput("measure", "", c("Duration", "Silence")) 
), 

    mainPanel(
    tags$div(class = "dashboard_main", 
      tags$div(class="dashboard_main_left", plotOutput("secondPlot")) 

    ) 
) 

) 
server <- function(input, output){ 

    output$secondPlot <- renderPlot({ 
    ggplot(data = df) + 
     geom_mosaic(aes(weight = Weight, x = product(Category), fill=Duration), 
        offset = 0, na.rm=TRUE) + 
     theme(axis.text.x=element_text(angle=-25, hjust= .1)) + 
     theme(axis.title.x=element_blank()) + 
     scale_fill_manual(values=c("#e8f5e9", "#c8e6c9", "#a5d6a7", "#81c784", "#66bb6a")) 
    }) 
} 

shinyApp(ui = ui, server= server) 

を。だからDurationを選択した場合、プロットの「secondPlot」はDurationでなければなりません。また、「Silence」を選択した場合は、「Silence」になります。私は、グラフのrelevanteコードを変更するとき

は、しかし:

ggplot(data = df) + 
     geom_mosaic(aes(weight = Weight, x = product(Category), fill=input$measure), 
        offset = 0, na.rm=TRUE) + 
     theme(axis.text.x=element_text(angle=-25, hjust= .1)) + 
     theme(axis.title.x=element_blank()) 

私はもう色のグラデーションを参照してくださいいけません。ここで何がうまくいかないのか?

答えて

3

aes_stringgeom_mosaicの範囲内で使用してください。試してみよう:

server <- function(input, output){ 
    df$prodcat <- product(df$Category) 
    output$secondPlot <- renderPlot({ 
    ggplot(data = df) + 
     geom_mosaic(aes_string(weight = "Weight", x = "prodcat", fill=input$measure), 
        offset = 0, na.rm=TRUE) + 
     theme(axis.text.x=element_text(angle=-25, hjust= .1)) + 
     theme(axis.title.x=element_blank()) + 
     scale_fill_manual(values=c("#e8f5e9", "#c8e6c9", "#a5d6a7", "#81c784", "#66bb6a")) 
    }) 
} 
関連する問題