2017-12-30 10 views
1

Ich habe folgende glänzend App:Ändern der Füllung von Mosaikplot in Shiny

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) 

Ich mag würde die zweite Handlung interaktive jetzt machen. Wenn Sie also die Dauer auswählen, sollte die Füllung in der Grafik "secondPlot" Dauer sein und wenn Sie "Silence" auswählen, sollte die Füllung "Silence" lauten.

Allerdings, wenn ich ändern Sie den Code relevanten der Grafik:

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

ich nicht mehr die Farbverläufe sehen. Irgendwelche Gedanken darüber, was hier schief geht?

Antwort

3

Sie sollten aes_string innerhalb geom_mosaic verwenden. Versuchen Sie dies:

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