I am trying to have a ggplot graph updated each time you select a different radio button, but it's not working quite right. What I want to have happen is the selection of the radio button change the color on the graph. There are three separate categories, so ideally, if the first button was selected: Category 1 would be green (or whatever color I choose) and Categories 2 and 3 would be the same colors, but different from 1. Here is my code:
# 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)
The bar chart moves around with each selection of the radio button and seems to group the two categories that are not selected. How do I keep the bar charts static but the color to react appropriately to the radio button selection?
Is there a better way I can go about doing this? Any help would be appreciated!
I have ignored your columns C1
, C2
and C3
and used Var2
as the fill
variable. Assuming that, is this what you are after?
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)
})
}
You could make the assignment of colors to scale_fill_manual
more robust in case you have more variation; this is just the simplest way to go.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.