简体   繁体   English

带有百分比的堆积条形图

[英]Stacked bar charts with percentages

I would like to create a stacked bar chart to show the adjusted percentages of specific data per country.我想创建一个堆积条形图来显示每个国家/地区特定数据的调整百分比。 Basically, I would like to adjust the bar charts to fit the percentages?基本上,我想调整条形图以适应百分比? Also how can change the number to percentages (eg. instead of -0.02 to show -2%?另外如何将数字更改为百分比(例如,而不是 -0.02 来显示 -2%?

Sample code:示例代码:

df$Country<- factor(df$Country,levels = c( "Australia","Austria", "Belgium","Canada","Denmark","Finland",
                                           "France","Germany","Greece","Iceland","Ireland","Italy",
                                           "Japan", "Luxembourg", "Netherlands","New Zealand","Norway","Portugal", 
                                           "Spain" ,"Sweden","Switzerland","Turkey", "United Kingdom", "United States" ))


df$Data<- factor(df$Data,levels = c("TED 2003 (adjusted)", "OECD 2003 (adjusted)"))


(plot<-ggplot(df, aes(x=THW, y=Country, fill=Data,label = THW)) +  
    geom_bar( stat = "identity") + 
    geom_text(size = 4,  position = position_stack(vjust = 0.5),color="black", fontface="bold")+
    #facet_wrap(~Country, scales = "free")+
    labs(x="Average hours worked per civilian employee in 2003", y="", title="", fill="Data source:" )+
    theme_bw()+
    
    theme(axis.text.x = element_text(angle = , hjust = 1,family="Times", face="bold", size=12, color="black"), 
          axis.title.x = element_text(family="Times", face="bold", size=16, color="black"),
          axis.text.y = element_text(family="Times", face="bold", size=12, color="black"),
          axis.title.y = element_text(family="Times", face="bold", size=16, color="black"),
          strip.text = element_text(size=15, face="bold"),
          plot.title = element_text(size=20, face="bold"))+
    theme(legend.title = element_text(family="Times", face="bold", size=16, color="black"),
          legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
          legend.position="right",
          legend.box = "vertical",
          plot.title = element_text(hjust = 0.5))+
    scale_y_discrete()+
    theme(axis.text.x=element_blank()))

Sample data 1 (totals):样本数据1(总计):

   df<-structure(list(Country = structure(c(17L, 15L, 7L, 8L, 2L, 14L, 
5L, 21L, 20L, 12L, 3L, 23L, 6L, 11L, 18L, 16L, 1L, 13L, 4L, 19L, 
10L, 24L, 9L, 22L, 17L, 15L, 7L, 8L, 2L, 14L, 5L, 21L, 20L, 12L, 
3L, 23L, 6L, 11L, 18L, 16L, 1L, 13L, 4L, 19L, 10L, 24L, 9L, 22L
), .Label = c("Australia", "Austria", "Belgium", "Canada", "Denmark", 
"Finland", "France", "Germany", "Greece", "Iceland", "Ireland", 
"Italy", "Japan", "Luxembourg", "Netherlands", "New Zealand", 
"Norway", "Portugal", "Spain", "Sweden", "Switzerland", "Turkey", 
"United Kingdom", "United States"), class = "factor"), THW = c(-0.05, 
-0.06, -0.05, 0.01, -0.19, -0.04, 0.04, -0.08, -0.02, -0.13, 
0.03, -0.03, -0.04, -0.14, -0.11, -0.04, -0.02, -0.04, 0.01, 
0.02, 0.09, 0.01, -0.08, -0.01, -0.04, -0.06, -0.05, 0, -0.2, 
-0.04, 0.04, -0.08, 0.08, -0.13, 0.01, 0.05, 0.01, -0.14, -0.02, 
-0.04, -0.02, -0.02, 0.01, 0.03, 0.09, 0.01, -0.04, -0.01), Data = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("TED 2003 (adjusted)", 
"OECD 2003 (adjusted)"), class = "factor")), row.names = c(NA, 
-48L), spec = structure(list(cols = list(Country = structure(list(), class = c("collector_character", 
"collector")), THW = structure(list(), class = c("collector_double", 
"collector")), Data = structure(list(), class = c("collector_character", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1L), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))


**Output**

在此处输入图像描述

Explanation:解释:

With ggplot 2.1.0, geom_text has position_fill / position_stack, Therefore, there is no longer a need to calculate nor use the y aesthetic pos to position the labels.使用 ggplot 2.1.0,geom_text 有 position_fill / position_stack,因此,不再需要计算或使用 y 美学 pos 到 position 标签。

  1. add a label column to your dataframe: df1 <- df %>% mutate(label = paste0(THW*100, "%"))在 dataframe 中添加label列: df1 <- df %>% mutate(label = paste0(THW*100, "%"))
  2. Then I changed x and y and used coord_flip() at the end.然后我改变了 x 和 y 并在最后使用coord_flip() I think this is more logic.我认为这更符合逻辑。 At least for me.至少对我来说。
  3. Add aes(label = label) to geom_text like: geom_text(aes(label = label), size = 3, position = position_stack(vjust = 0.5),color="black", fontface="bold") +aes(label = label)添加到geom_text ,例如: geom_text(aes(label = label), size = 3, position = position_stack(vjust = 0.5),color="black", fontface="bold") +

Full code:完整代码:

df$Country<- factor(df$Country,levels = c( "Australia","Austria", "Belgium","Canada","Denmark","Finland",
                                           "France","Germany","Greece","Iceland","Ireland","Italy",
                                           "Japan", "Luxembourg", "Netherlands","New Zealand","Norway","Portugal", 
                                           "Spain" ,"Sweden","Switzerland","Turkey", "United Kingdom", "United States" ))


df$Data<- factor(df$Data,levels = c("TED 2003 (adjusted)", "OECD 2003 (adjusted)"))

df1 <- df %>%  mutate(label = paste0(THW*100, "%"))

(plot<-ggplot(df1, aes(x=Country, y=THW, fill=Data,label = THW)) +  
    geom_bar( stat = "identity") + 
    geom_text(aes(label = label), size = 3,  position = position_stack(vjust = 0.5),color="black", fontface="bold") +
    #facet_wrap(~Country, scales = "free")+
    labs(x="Average hours worked per civilian employee in 2003", y="", title="", fill="Data source:" )+
    theme_bw()+
    
    theme(axis.text.x = element_text(angle = , hjust = 1,family="Times", face="bold", size=12, color="black"), 
          axis.title.x = element_text(family="Times", face="bold", size=16, color="black"),
          axis.text.y = element_text(family="Times", face="bold", size=12, color="black"),
          axis.title.y = element_text(family="Times", face="bold", size=16, color="black"),
          strip.text = element_text(size=15, face="bold"),
          plot.title = element_text(size=20, face="bold"))+
    theme(legend.title = element_text(family="Times", face="bold", size=16, color="black"),
          legend.text = element_text(family="Times", color = "black", size = 16,face="bold"),
          legend.position="right",
          legend.box = "vertical",
          plot.title = element_text(hjust = 0.5))+
    scale_x_discrete()+
    coord_flip() +
    theme(axis.text.x=element_blank()))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM