我想创建一个堆积条形图来显示每个国家/地区特定数据的调整百分比。基本上,我想调整条形图以适应百分比?另外如何将数字更改为百分比(例如,而不是 -0.02 显示 -2%?
示例代码:
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()))
示例数据 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**
答案 0 :(得分:1)
说明:
使用 ggplot 2.1.0,geom_text 具有 position_fill / position_stack,因此不再需要计算或使用 y 美学 pos 来定位标签。
label
列:
df1 <- df %>% mutate(label = paste0(THW*100, "%"))
coord_flip()
。我认为这更符合逻辑。至少对我来说是这样。aes(label = label)
添加到 geom_text
,例如:
geom_text(aes(label = label), size = 3, position = position_stack(vjust = 0.5),color="black", fontface="bold") +
完整代码:
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()))