Home > OS >  R geom_bar: How can I return the Y-axis title to my graph?
R geom_bar: How can I return the Y-axis title to my graph?

Time:05-22

I am working on creating a geom_bar based on the built in esoph dataset in Rstudio. At first, the graph had both its x- and y-axis title. However, as I try to format the title, the Y-axis title went missing. I tried changing the size of the title but it never showed up again. Anyone had any idea how to fix it ?

data(esoph)
data_esoph <- esoph %>% group_by(agegp) %>% 
              summarise(Total_number_of_control_case = sum(ncontrols)) %>% 
              rename(Age_group = agegp)
plot_bar <- data_esoph %>% ggplot(aes(x = Age_group, y = Total_number_of_control_case, fill = Age_group))  
            geom_bar(stat = "identity", position = "dodge")  
            labs(title ="Number of control cases by age group", subtitle = "Data source: built-in esoph dataset", x = "Age group", y = "Cases", fill = "Age group")  
            geom_text(aes(label = Total_number_of_control_case), vjust = 1.5, colour = "white")  
            theme_cleveland()  
            theme(plot.title = element_text(face = "bold", size = 16, hjust = 0.5), plot.subtitle = element_text(face = "italic", size = 10, hjust = 0.5),
                  axis.title = element_text(face = "bold", size = 10), legend.title = element_text(face = "bold", size = 10))   
            scale_fill_jama() 
print(plot_bar)

CodePudding user response:

It looks the default for theme_cleveland() is to remove the y-axis label. Are you sure you need that theme? If you must, you'll need to specify the axis.title.y() manually:

data(esoph)
library(dplyr)
library(ggplot2)
library(ggsci)
library(ggpubr)

data_esoph <- esoph %>% group_by(agegp) %>% 
  summarise(Total_number_of_control_case = sum(ncontrols)) %>% 
  rename(Age_group = agegp)

data_esoph %>% ggplot(aes(x = Age_group, y = Total_number_of_control_case, fill = Age_group))  
  geom_bar(stat = "identity", position = "dodge")  
  labs(title ="Number of control cases by age group", subtitle = "Data source: built-in esoph dataset", x = "Age group", y = "Cases", fill = "Age group")  
  geom_text(aes(label = Total_number_of_control_case), vjust = 1.5, colour = "white")  
  ggpubr::theme_cleveland()  
  ggsci::scale_fill_jama()  
  theme(plot.title = element_text(face = "bold", size = 16, hjust = 0.5),
        plot.subtitle = element_text(face = "italic", size = 10, hjust = 0.5),
        axis.title = element_text(face = "bold", size = 10),
        legend.title = element_text(face = "bold", size = 10),
        axis.title.y = element_text())

enter image description here

  • Related