簡體   English   中英

R geom_bar:如何將 Y 軸標題返回到我的圖表?

[英]R geom_bar: How can I return the Y-axis title to my graph?

我正在基於 Rstudio 中內置的 esoph 數據集創建 geom_bar。 起初,該圖同時具有 x 軸和 y 軸標題。 但是,當我嘗試格式化標題時,Y 軸標題丟失了。 我嘗試更改標題的大小,但它再也沒有出現。 有人知道如何解決嗎?

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)

看起來 theme_cleveland() 的默認值是刪除 y 軸標簽。 你確定你需要那個主題嗎? 如果必須,您需要手動指定axis.title.y():

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())

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM