簡體   English   中英

R ggplot2 添加額外的 x 軸標簽

[英]R ggplot2 add additional x-axis labels

根據下面的代碼和數據,有沒有辦法在每10%之后添加15%以顯示x-axis的值大於/小於或等於+/- 15%

請注意,其中一個數據集的Value列中沒有15

我嘗試將scale_x_discretelimits參數一起使用,但它不起作用。

兩個圖上所需x-axis順序:

15% 10% 0 10% 15%

數據( pop_hisp_df ):

structure(list(age_group = c("<  5 years", "5 - 14", "15  -  24", 
"25  -  34", "35  -  44", "45  -  54", "55  -  64", "65  -  74", 
"75  -  84", "85 +", "<  5 years", "5 - 14", "15  -  24", "25  -  34", 
"35  -  44", "45  -  54", "55  -  64", "65  -  74", "75  -  84", 
"85 +"), Type = c("Males", "Males", "Males", "Males", "Males", 
"Males", "Males", "Males", "Males", "Males", "Females", "Females", 
"Females", "Females", "Females", "Females", "Females", "Females", 
"Females", "Females"), Value = c(-6, -13, -13, -15, -17, -15, 
-11, -6, -3, -1, 6, 12, 12, 14, 16, 15, 12, 7, 4, 2)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

pop_gen_df

structure(list(age_group = c("<  5 years", "5 - 14", "15  -  24", 
"25  -  34", "35  -  44", "45  -  54", "55  -  64", "65  -  74", 
"75  -  84", "85 +", "<  5 years", "5 - 14", "15  -  24", "25  -  34", 
"35  -  44", "45  -  54", "55  -  64", "65  -  74", "75  -  84", 
"85 +"), Type = c("Males", "Males", "Males", "Males", "Males", 
"Males", "Males", "Males", "Males", "Males", "Females", "Females", 
"Females", "Females", "Females", "Females", "Females", "Females", 
"Females", "Females"), Value = c(-6, -12, -12, -14, -13, -14, 
-13, -9, -4, -2, 6, 11, 11, 13, 13, 14, 13, 10, 5, 3)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

代碼:

    library(tidyverse)
    library(plotly)
    
    # Plot
    gg_pop_hisp = ggplot(pop_hisp_df, aes( x = forcats::as_factor(age_group), y = Value, fill = Type)) +
      geom_bar(data = subset(pop_hisp_df, Type == "females"), stat = "identity") + 
      geom_bar(data = subset(pop_hisp_df, Type == "males"), stat = "identity") + 
      scale_y_continuous(labels = function(z) paste0(abs(z), "%")) +          # CHANGE
      scale_fill_manual(name = "", values = c("females"="#FC921F", "males"="#149ECE"), labels = c("Females", "Males")) +
      ggtitle("HISPANIC POPULATION BY GENDER AND AGE GROUP") +
      labs(x = "PERCENTAGE POPULATION", y = "AGE GROUPS", fill = "Gender") +
      theme_minimal() +
      theme(legend.position="bottom") +
      coord_flip()

gg_pop_gen = ggplot(pop_gen_df, aes(x = forcats::as_factor(age_group), y = Value, fill = Type))  +
  geom_bar(data = subset(pop_hisp_df, Type == "Females"), stat = "identity") + 
  geom_bar(data = subset(pop_hisp_df, Type == "Males"), stat = "identity") + 
  scale_y_continuous(labels = function(z) paste0(abs(z), "%")) +          # CHANGE
  scale_fill_manual(name = "", values = c("Females"="#ED5151", "Males"="#6B6BD6"), labels = c("Females", "Males")) +
  ggtitle("TOTAL POPULATION BY AGE AND GENDER") +
  labs(x = "PERCENTAGE POPULATION", y = "AGE GROUPS", fill = "Gender") +
  theme_minimal() +
  theme(legend.position="bottom") +
  coord_flip()
    
    # Interactive and place legend at the bottom
    ggplotly(gg_pop_hisp) %>% 
      layout(
        legend = list(
          orientation = 'h', x = 0.3, y = -0.1, 
          title = list(text = '')
          )
        )

ggplotly(gg_pop_gen) %>% 
  layout(
    legend = list(
      orientation = 'h', x = 0.3, y = -0.3, 
      title = list(text = '')
      )
    )

您可以將兩個圖的scale_y_continuous更改為:

  scale_y_continuous(
    limits=c(-20,20),
    breaks=c(-15,-10,0,10,15),
    labels=paste0(c(15,10,0,10,15),"%")
  )

完整代碼:

library(tidyverse)
library(plotly)

# Plot
gg_pop_hisp = ggplot(pop_hisp_df, aes( x = forcats::as_factor(age_group), y = Value, fill = Type)) +
  geom_bar(data = subset(pop_hisp_df, Type == "Females"), stat = "identity") + 
  geom_bar(data = subset(pop_hisp_df, Type == "Males"), stat = "identity") + 
  #scale_y_continuous(labels = function(z) paste0(abs(z), "%")) +          # CHANGE
  scale_y_continuous(
    limits=c(-20,20),
    breaks=c(-15,-10,0,10,15),
    labels=paste0(c(15,10,0,10,15),"%")
  ) + 
  scale_fill_manual(name = "", values = c("Females"="#FC921F", "Males"="#149ECE"), labels = c("Females", "Males")) +
  ggtitle("HISPANIC POPULATION BY GENDER AND AGE GROUP") +
  labs(x = "PERCENTAGE POPULATION", y = "AGE GROUPS", fill = "Gender") +
  theme_minimal() +
  theme(legend.position="bottom") +
  coord_flip()

gg_pop_hisp

gg_pop_gen = ggplot(pop_gen_df, aes(x = forcats::as_factor(age_group), y = Value, fill = Type))  +
  geom_bar(data = subset(pop_gen_df, Type == "Females"), stat = "identity") + 
  geom_bar(data = subset(pop_gen_df, Type == "Males"), stat = "identity") + 
  #scale_y_continuous(labels = function(z) paste0(abs(z), "%")) +          # CHANGE
  scale_y_continuous(
    limits=c(-20,20),
    breaks=c(-15,-10,0,10,15),
    labels=paste0(c(15,10,0,10,15),"%")
  ) + 
  scale_fill_manual(name = "", values = c("Females"="#ED5151", "Males"="#6B6BD6"), labels = c("Females", "Males")) +
  ggtitle("TOTAL POPULATION BY AGE AND GENDER") +
  labs(x = "PERCENTAGE POPULATION", y = "AGE GROUPS", fill = "Gender") +
  theme_minimal() +
  theme(legend.position="bottom") +
  coord_flip()

# Interactive and place legend at the bottom
ggplotly(gg_pop_hisp) %>% 
  layout(
    legend = list(
      orientation = 'h', x = 0.3, y = -0.1, 
      title = list(text = '')
    )
  )

ggplotly(gg_pop_gen) %>% 
  layout(
    legend = list(
      orientation = 'h', x = 0.3, y = -0.3, 
      title = list(text = '')
    )
  )

暫無
暫無

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

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