简体   繁体   English

R ggplot2 geom_bar 添加带 % 符号的标签

[英]R ggplot2 geom_bar add labels with % sign

Based on the data and code below, I want to add labels to the pyramid plot based on the Value column, add a % sign next to the value and remove the - sign from the label values.根据下面的数据和代码,我想根据Value列向pyramid plot添加标签,在值旁边添加%符号并从 label 值中删除-符号。

Right now the labels are appearing on the x-axis without the % sign and with the - sign.现在标签出现在没有%符号和-符号的x-axis How can I fix this?我怎样才能解决这个问题?

Current output:当前 output:

在此处输入图像描述

Data ( pop_hisp_df ):数据( 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"))

Code:代码:

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_text(aes(label = Value, "%")) + 
          geom_bar(data = subset(pop_hisp_df, Type == "Males"), stat = "identity") +
          scale_y_continuous(limits=c(-20,20),
                             breaks=c(-15,-10,0,10,15),
                             labels=paste0(c(15,10,0,10,15),"%")) +          # 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 = "AGE GROUPS", y = "PERCENTAGE POPULATION", fill = "Gender") +
          theme_minimal() +
          theme(legend.position="bottom") +
          coord_flip()  
        
        # Interactive
        ggplotly(gg_pop_hisp) %>% 
          layout(
            legend = list(
              orientation = 'h', x = 0.3, y = -0.3, 
              title = list(text = '')

You had a typo in the geom_text aes (you forgot a paste0 ), which caused the % to be interpreted as the desired y-value.您在geom_text aes中有错字(您忘记了paste0 ),这导致%被解释为所需的 y 值。 Adding a paste0() fixes it.添加paste0()修复它。 I needed to do abs in order for the minus sign to disappear on the left-hand side.我需要做abs以使减号在左侧消失。

# 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") +
  geom_text(aes(label = paste0(abs(Value), "%"))) + 
  scale_y_continuous(limits=c(-20,20),
                     breaks=c(-15,-10,0,10,15),
                     labels=paste0(c(15,10,0,10,15),"%")) +          # 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 = "AGE GROUPS", y = "PERCENTAGE POPULATION", fill = "Gender") +
  theme_minimal() +
  theme(legend.position="bottom") +
  coord_flip()  

# Interactive
ggplotly(gg_pop_hisp) %>% 
  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