I know this has been answered numerous times and I also found a detailed explanation about position_dodge to align the bar graph labels in this post What is the width argument in position_dodge?
But for some reason, I am not able to figure out the dodge position for my situation. I am creating a reactive dataset to get a count by a metric which is selected from a drop down menu and this is then passed to ggplot2, below is my code. input$qtr and input$met are selected by user.
library(readxl)
library(shiny)
library(ggplot2)
library(dplyr)
library(zoo)
library(shinydashboard)
data ("mtcars")
df$Qtr <- ifelse(mtcars$am==1, "2018Q1","2018Q2")
df$Responsibility <- ifelse(mtcars$gear %in% c(3, 4), "Category 1","Category
2")
df$Leader <- ifelse(mtcars$vs==0, "No","Yes")
df$Failure <- ifelse(mtcars$carb %in% c(1, 2), "Quality","Timing")
df$Cname <- ifelse(mtcars$carb %in% c(1, 2), "Company 1","Company 2")
df <- df[,c("Cname", "Responsibility", "Qtr", "Leader","Failure")]
ui <- dashboardPage(skin="blue",
dashboardHeader(title = "R Shiny Concept",titleWidth = 200),
# Sidebar layout with a input and output definitions
dashboardSidebar(id="", sidebarMenu(uiOutput("qtr"),
menuItem("All", tabName = "all", icon = icon("bars")))),
dashboardBody (tabItems(tabItem(tabName = "all",
fluidRow(uiOutput("met")),
fluidRow(plotOutput(outputId = "metrics"))))))
server <- function(input, output, session) {
# Drop-down selection box for quarter
output$qtr <- renderUI({selectInput(inputId = "qtr",
label = "Pick a Quarter",
choices= as.list(gsub(" ","",df$Qtr)),
selected = 1)})
# Drop-down selection box for metrics
output$met <- renderUI({selectInput(inputId = "met",
label = "Pick a metrics to report",
choices= c("Responsibility",
"Leader",
"Failure"),
selected = 1)})
# Create a subset of data filtering for selected CRO
freq_subset2 <- reactive({
req(input$qtr)
req(input$met)
df %>%
group_by_at(vars(Cname, Qtr,(input$met))) %>%
select(Cname, Qtr,(input$met)) %>%
summarise(count = n()) %>%
filter(gsub(" ","",Qtr) %in% input$qtr)
})
plot3 <- output$metrics <- renderPlot({
ggplot(data = freq_subset2(), aes(x=Cname, y=count)) +
labs(y=" ", x = " ")+
geom_bar(stat="identity", position = "dodge", width=0.8, aes_string(fill =
(input$met))) +
geom_text(aes(label=count), color="black",position =
position_dodge(width=0.8),
hjust = 1.5, size=3.5) +scale_fill_brewer(palette="Set1") +
theme(axis.ticks = element_blank(),axis.text.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),panel.background= element_blank(),
plot.title = element_text(size = rel(1.5), face = "bold"),
legend.position="bottom",legend.title = element_text(color = "white"))
})
plot3
}
# Create the Shiny app object
shinyApp(ui = ui, server = server)
Here is the output enter image description here