ggplot2:如何将 geom_text 放在 bar 前面?
ggplot2: How to bring geom_text in front of bar?
我想使用 geom_text()
我的图表,但它显示在我的条形图后面。
如何将它放在柱子前面以及如何将总金额显示为 geom_text()
?
你会在我的数据集下方找到(编辑)
BRNCH_NAME PRODUCTS FINAL_SCORE
1 A ANCH BILL 1998
2 B ANCH BILL 1216
7 G ANCH BILL 1220
8 H ANCH BILL 1217
16 A ANCH TICKET 1998
17 B ANCH TICKET 2331
18 C ANCH TICKET 0
19 D ANCH TICKET 0
20 E ANCH TICKET 0
21 F ANCH TICKET 0
22 G ANCH TICKET 1665
31 A ANCH SHOP 2331
32 B ANCH SHOP 1154
33 C ANCH SHOP 1165
34 D ANCH SHOP 1388
35 E ANCH SHOP 1265
ggplot(data = FINAL_SCORE_BAR,
aes(x = reorder(FINAL_SCORE_BAR$BRNCH_NAME, FINAL_SCORE_BAR$FINAL_SCORE),
y = FINAL_SCORE, fill = PRODUCTS, label = FINAL_SCORE)) +
geom_text(size = 3, position = position_stack(vjust = 0.5))+
labs(y = "HGA ALL FINAL SCORE", x = "ELEMENTS", face = "bold",
colour = "black", csize = 5) +
coord_flip() +
geom_bar(stat = "identity", position = "stack")+
theme(axis.text.x = element_text(size = 12, face = "bold"),
axis.text.y = element_text(size = 12, face = "bold")) +
scale_fill_brewer(palette = "Blues")
更改几何的顺序,例如:
ggplot(data = FINAL_SCORE_BAR,
aes(x = reorder(FINAL_SCORE_BAR$BRNCH_NAME, FINAL_SCORE_BAR$FINAL_SCORE),
y = FINAL_SCORE, fill = PRODUCTS, label = FINAL_SCORE)) +
labs(y = "HGA ALL FINAL SCORE", x = "ELEMENTS", face = "bold",
colour = "black", csize = 5) +
coord_flip() +
geom_bar(stat = "identity", position = "stack") +
theme(axis.text.x = element_text(size = 12, face = "bold"),
axis.text.y = element_text(size = 12, face = "bold")) +
scale_fill_brewer(palette = "Blues") +
# the order of geoms matters
geom_text(size = 3, position = position_stack(vjust = 0.5))
UPDATE 根据问题中的更新信息。
您的数据中缺少第 "PRODUCTS" 列。没有它我的答案是这样的(如果你没有它们,你应该安装 data.table
和 tidyverse
包:
library(tidyverse)
FINAL_SCORE_BAR <-
data.table::fread(stringsAsFactors = TRUE,
'BRNCH_NAME ELEMENTS FINAL_SCORE
"A ANCH" BILL 1998
"B ANCH" BILL 1216
"G ANCH" BILL 1220
"H ANCH" BILL 1217
"A ANCH" TICKET 1998
"B ANCH" TICKET 2331
"C ANCH" TICKET 0
"D ANCH" TICKET 0
"E ANCH" TICKET 0
"F ANCH" TICKET 0
"G ANCH" TICKET 1665
"A ANCH" SHOP 2331
"B ANCH" SHOP 1154
"C ANCH" SHOP 1165
"D ANCH" SHOP 1388
"E ANCH" SHOP 1265')
FINAL_SCORE_BAR <- FINAL_SCORE_BAR %>%
mutate(BRNCH_NAME = reorder(BRNCH_NAME, FINAL_SCORE, FUN = mean))
my_sum <- FINAL_SCORE_BAR %>%
group_by(BRNCH_NAME) %>%
summarize(SUM_FINAL_SCORE = sum(FINAL_SCORE))
ggplot(data = FINAL_SCORE_BAR,
aes(x = BRNCH_NAME, y = FINAL_SCORE)) +
labs(y = "HGA ALL FINAL SCORE", x = "ELEMENTS", face = "bold",
colour = "black", csize = 5) +
coord_flip(ylim = c(0, 7500)) +
geom_bar(stat = "identity", position = "stack")+
theme(axis.text.x = element_text(size = 12, face = "bold"),
axis.text.y = element_text(size = 12, face = "bold")) +
scale_fill_brewer(palette = "Blues") +
# The sums:
geom_text(data = my_sum,
aes(x = BRNCH_NAME,
y = SUM_FINAL_SCORE,
label = SUM_FINAL_SCORE),
size = 3,
hjust = -0.2,
inherit.aes = FALSE
)
我想使用 geom_text()
我的图表,但它显示在我的条形图后面。
如何将它放在柱子前面以及如何将总金额显示为 geom_text()
?
你会在我的数据集下方找到(编辑)
BRNCH_NAME PRODUCTS FINAL_SCORE
1 A ANCH BILL 1998
2 B ANCH BILL 1216
7 G ANCH BILL 1220
8 H ANCH BILL 1217
16 A ANCH TICKET 1998
17 B ANCH TICKET 2331
18 C ANCH TICKET 0
19 D ANCH TICKET 0
20 E ANCH TICKET 0
21 F ANCH TICKET 0
22 G ANCH TICKET 1665
31 A ANCH SHOP 2331
32 B ANCH SHOP 1154
33 C ANCH SHOP 1165
34 D ANCH SHOP 1388
35 E ANCH SHOP 1265
ggplot(data = FINAL_SCORE_BAR,
aes(x = reorder(FINAL_SCORE_BAR$BRNCH_NAME, FINAL_SCORE_BAR$FINAL_SCORE),
y = FINAL_SCORE, fill = PRODUCTS, label = FINAL_SCORE)) +
geom_text(size = 3, position = position_stack(vjust = 0.5))+
labs(y = "HGA ALL FINAL SCORE", x = "ELEMENTS", face = "bold",
colour = "black", csize = 5) +
coord_flip() +
geom_bar(stat = "identity", position = "stack")+
theme(axis.text.x = element_text(size = 12, face = "bold"),
axis.text.y = element_text(size = 12, face = "bold")) +
scale_fill_brewer(palette = "Blues")
更改几何的顺序,例如:
ggplot(data = FINAL_SCORE_BAR,
aes(x = reorder(FINAL_SCORE_BAR$BRNCH_NAME, FINAL_SCORE_BAR$FINAL_SCORE),
y = FINAL_SCORE, fill = PRODUCTS, label = FINAL_SCORE)) +
labs(y = "HGA ALL FINAL SCORE", x = "ELEMENTS", face = "bold",
colour = "black", csize = 5) +
coord_flip() +
geom_bar(stat = "identity", position = "stack") +
theme(axis.text.x = element_text(size = 12, face = "bold"),
axis.text.y = element_text(size = 12, face = "bold")) +
scale_fill_brewer(palette = "Blues") +
# the order of geoms matters
geom_text(size = 3, position = position_stack(vjust = 0.5))
UPDATE 根据问题中的更新信息。
您的数据中缺少第 "PRODUCTS" 列。没有它我的答案是这样的(如果你没有它们,你应该安装 data.table
和 tidyverse
包:
library(tidyverse)
FINAL_SCORE_BAR <-
data.table::fread(stringsAsFactors = TRUE,
'BRNCH_NAME ELEMENTS FINAL_SCORE
"A ANCH" BILL 1998
"B ANCH" BILL 1216
"G ANCH" BILL 1220
"H ANCH" BILL 1217
"A ANCH" TICKET 1998
"B ANCH" TICKET 2331
"C ANCH" TICKET 0
"D ANCH" TICKET 0
"E ANCH" TICKET 0
"F ANCH" TICKET 0
"G ANCH" TICKET 1665
"A ANCH" SHOP 2331
"B ANCH" SHOP 1154
"C ANCH" SHOP 1165
"D ANCH" SHOP 1388
"E ANCH" SHOP 1265')
FINAL_SCORE_BAR <- FINAL_SCORE_BAR %>%
mutate(BRNCH_NAME = reorder(BRNCH_NAME, FINAL_SCORE, FUN = mean))
my_sum <- FINAL_SCORE_BAR %>%
group_by(BRNCH_NAME) %>%
summarize(SUM_FINAL_SCORE = sum(FINAL_SCORE))
ggplot(data = FINAL_SCORE_BAR,
aes(x = BRNCH_NAME, y = FINAL_SCORE)) +
labs(y = "HGA ALL FINAL SCORE", x = "ELEMENTS", face = "bold",
colour = "black", csize = 5) +
coord_flip(ylim = c(0, 7500)) +
geom_bar(stat = "identity", position = "stack")+
theme(axis.text.x = element_text(size = 12, face = "bold"),
axis.text.y = element_text(size = 12, face = "bold")) +
scale_fill_brewer(palette = "Blues") +
# The sums:
geom_text(data = my_sum,
aes(x = BRNCH_NAME,
y = SUM_FINAL_SCORE,
label = SUM_FINAL_SCORE),
size = 3,
hjust = -0.2,
inherit.aes = FALSE
)