Home > OS >  ggplot Bar Chart with empty tick marks
ggplot Bar Chart with empty tick marks

Time:07-23

I am creating a shiny app to track the yearly budget of a sector over 5 years. The first two years are submitted and plotted as a bar chart but I want to show the remaining five years on the x axis without putting 0's in the dataframe, as they submit their budget yearly through a form on a shiny app. If I added 0's to the dataframe when they submit their budget it would not autopopulate and display the correct chart which is the purpose of the shiny app.

Current data frame:

ACSyear ACSbudget
FY22 10000
FY23 15000

Current code:

library(ggplot2)

ggplot(ACSbudgdata, aes(ACSyear, ACSbudget))  
      geom_bar(stat = "identity")  
      ylab("Budget Amount (dollars)")   xlab("Fiscal Year")  
      theme(legend.position = "right")  
      scale_y_continuous(label = comma)  
      ggtitle("Strategic Plan Tracking: Aquatic Conservation Budget") 

What I would like the chart to look like while keeping the dataframe the same:

enter image description here

CodePudding user response:

One option would be to set categories to be displayed via the limits argument of scale_x_discrete:

library(ggplot2)

ACSbudgdata <- data.frame(
           ACSyear = c("FY22", "FY23"),
         ACSbudget = c(10000L, 15000L)
)

ggplot(ACSbudgdata, aes(ACSyear, ACSbudget))  
  geom_bar(stat = "identity")  
  ylab("Budget Amount (dollars)")   xlab("Fiscal Year")  
  theme(legend.position = "right")  
  scale_y_continuous(label = scales::comma)  
  scale_x_discrete(limits = paste0("FY", 22:26))  
  ggtitle("Strategic Plan Tracking: Aquatic Conservation Budget") 

  • Related