根据单选按钮选择更改ggplot的颜色(R)

根据单选按钮选择更改ggplot的颜色(R),r,ggplot2,shiny,radio-button,reactive,R,Ggplot2,Shiny,Radio Button,Reactive,我试图在每次选择不同的单选按钮时更新ggplot图,但它不太正常。我想做的是选择单选按钮,改变图表上的颜色。有三个独立的类别,因此理想情况下,如果选择第一个按钮:类别1将是绿色或我选择的任何颜色,类别2和3将是相同的颜色,但不同于1。这是我的密码: # Define UI for application ui = fluidPage( navbarPage("Example Problem", tabPanel(&quo

我试图在每次选择不同的单选按钮时更新ggplot图,但它不太正常。我想做的是选择单选按钮,改变图表上的颜色。有三个独立的类别,因此理想情况下,如果选择第一个按钮:类别1将是绿色或我选择的任何颜色,类别2和3将是相同的颜色,但不同于1。这是我的密码:

# Define UI for application
ui = fluidPage(
  navbarPage("Example Problem",
            
             tabPanel("Example",
                      tabname="example",
                      icon=icon("thumbs-up"),
                      prettyRadioButtons(inputId = "rb", 
                                         label = "Make a selection:",
                                         c("3","4","5"),
                                         animation = "pulse",
                                         inline=TRUE),               
                      plotOutput("plot_example")
             )
                      ))


# Define server logic 
server <- function(input, output) {

  
  output$plot_example=renderPlot({
    df=as.data.frame(table(mtcars$carb,mtcars$gear))
    df$C3=ifelse(df$Var2==3,1,0)
    df$C4=ifelse(df$Var2==4,1,0)
    df$C5=ifelse(df$Var2==5,1,0)

    
    switch(input$rb,
           
           "3"=ggplot(data=df, aes(x=Var1, y=Freq, fill=factor(C3))) + geom_bar(stat="identity", position=position_dodge())+
             scale_fill_brewer(palette="Paired")+geom_text(aes(label=Freq), position=position_dodge(width=0.9), vjust=-0.25),
           "4"=ggplot(data=df, aes(x=Var1, y=Freq, fill=factor(C4))) + geom_bar(stat="identity", position=position_dodge())+
             scale_fill_brewer(palette="Paired")+geom_text(aes(label=Freq), position=position_dodge(width=0.9), vjust=-0.25),
           "5"=ggplot(data=df, aes(x=Var1, y=Freq, fill=factor(C5))) + geom_bar(stat="identity", position=position_dodge())+
             scale_fill_brewer(palette="Paired")+geom_text(aes(label=Freq), position=position_dodge(width=0.9), vjust=-0.25))
  })
  
  
}

# Run the application 
shinyApp(ui = ui, server = server)
条形图随着单选按钮的每次选择而移动,似乎将未选择的两个类别分组。如何使条形图保持静态,但颜色对单选按钮选择做出适当反应


有没有更好的方法可以让我这样做?任何帮助都将不胜感激

我忽略了C1、C2和C3列,并使用Var2作为填充变量。假设这样,这就是你想要的吗

server <- function(input, output) {
  
  output$plot_example = renderPlot({
    df = as.data.frame(table(mtcars$carb, mtcars$gear))
    
    colors <- switch(
      input$rb,
      "3" = c("green", "grey", "grey"),
      "4" = c("grey", "green", "grey"),
      "5" = c("grey", "grey", "green")
    )

    ggplot(data = df, aes(x = Var1, y = Freq, fill = factor(Var2))) +
      geom_bar(stat = "identity", position = position_dodge(), colour = "black") +
      scale_fill_manual(values = colors) +
      geom_text(aes(label = Freq),
                position = position_dodge(width = 0.9),
                vjust = -0.25)
  })
}

如果你有更多的变化,你可以将颜色分配到缩放填充手册中,使其更加稳定;这只是最简单的方法。

我忽略了C1、C2和C3列,使用Var2作为填充变量。假设这样,这就是你想要的吗

server <- function(input, output) {
  
  output$plot_example = renderPlot({
    df = as.data.frame(table(mtcars$carb, mtcars$gear))
    
    colors <- switch(
      input$rb,
      "3" = c("green", "grey", "grey"),
      "4" = c("grey", "green", "grey"),
      "5" = c("grey", "grey", "green")
    )

    ggplot(data = df, aes(x = Var1, y = Freq, fill = factor(Var2))) +
      geom_bar(stat = "identity", position = position_dodge(), colour = "black") +
      scale_fill_manual(values = colors) +
      geom_text(aes(label = Freq),
                position = position_dodge(width = 0.9),
                vjust = -0.25)
  })
}

如果你有更多的变化,你可以将颜色分配到缩放填充手册中,使其更加稳定;这是最简单的方法。

非常有效!谢谢你,保罗!很好用!谢谢你,保罗!