Using the below dataset, I have created the following plot. I wonder to know how can I round the labels. I tried to use the round() function with the format() function, but it did not work at the same time. My code is also available below.
Thanks, MRR
RACE<-ggplot(total_RACE_Reshape, aes(x = YEAR, y = Percent, fill = Race))
geom_col()
geom_text(aes(y= Percent,label = paste0(format(Percent), "%")), color= "white", position= position_stack(), hjust=1)
scale_fill_manual(values =c( "#F76900", "#000E54"))
labs(
x = "Year",
y = "Population 65 (%)",
caption = (""),
face = "bold"
)
scale_x_continuous(breaks = unique(total_RACE_Reshape$YEAR))
theme_classic()
coord_flip()
RACE
ggsave(file="RACE.png", width = 300, height = 225, units = "mm", dpi=1000)
data:
structure(list(YEAR = c(3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9,
9, 10, 10, 11, 11, 12, 12), TOT_POP.x = c(19399878, 19399878,
19499241, 19499241, 19572932, 19572932, 19624447, 19624447, 19651049,
19651049, 19654666, 19654666, 19633428, 19633428, 19589572, 19589572,
19530351, 19530351, 19453561, 19453561), TOT_MALE.x = c(9389210,
9389210, 9442775, 9442775, 9485138, 9485138, 9514647, 9514647,
9529710, 9529710, 9534954, 9534954, 9530062, 9530062, 9511544,
9511544, 9484218, 9484218, 9447846, 9447846), TOT_FEMALE.x = c(10010668,
10010668, 10056466, 10056466, 10087794, 10087794, 10109800, 10109800,
10121339, 10121339, 10119712, 10119712, 10103366, 10103366, 10078028,
10078028, 10046133, 10046133, 10005715, 10005715), both_sex_non_HispanicWhite.x = c(11338096,
11338096, 11310955, 11310955, 11274238, 11274238, 11227321, 11227321,
11168142, 11168142, 11093566, 11093566, 11010298, 11010298, 10931678,
10931678, 10847252, 10847252, 10755420, 10755420), both_sex_non_HispanicBlack.x = c(2829157,
2829157, 2843076, 2843076, 2851188, 2851188, 2858996, 2858996,
2860488, 2860488, 2858048, 2858048, 2850771, 2850771, 2838363,
2838363, 2825732, 2825732, 2813773, 2813773), TOT_POP.y = c(2628878,
2628878, 2672019, 2672019, 2767601, 2767601, 2844931, 2844931,
2923728, 2923728, 2998413, 2998413, 3073312, 3073312, 3148544,
3148544, 3221825, 3221825, 3296146, 3296146), TOT_MALE.y = c(1090103,
1090103, 1112837, 1112837, 1160474, 1160474, 1199530, 1199530,
1238470, 1238470, 1275521, 1275521, 1312526, 1312526, 1350034,
1350034, 1386274, 1386274, 1422971, 1422971), TOT_FEMALE.y = c(1538775,
1538775, 1559182, 1559182, 1607127, 1607127, 1645401, 1645401,
1685258, 1685258, 1722892, 1722892, 1760786, 1760786, 1798510,
1798510, 1835551, 1835551, 1873175, 1873175), both_sex_non_HispanicWhite.y = c(1920137,
1920137, 1933997, 1933997, 1993023, 1993023, 2034275, 2034275,
2075531, 2075531, 2109624, 2109624, 2146646, 2146646, 2184790,
2184790, 2221537, 2221537, 2257675, 2257675), both_sex_non_HispanicBlack.y = c(303534,
303534, 311197, 311197, 322243, 322243, 332511, 332511, 343511,
343511, 355278, 355278, 366038, 366038, 376362, 376362, 386273, 386273, 396670, 396670), population_65_percent = c(13.5510027434193,
13.5510027434193, 13.7031949089711, 13.7031949089711, 14.1399408121379,
14.1399408121379, 14.4968721921183, 14.4968721921183, 14.8782286380742,
14.8782286380742, 15.2554767402305, 15.2554767402305, 15.6534661191107,
15.6534661191107, 16.0725512532892, 16.0725512532892, 16.4965033142518,
16.4965033142518, 16.943663938957, 16.943663938957), Race = c("non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent", "non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent", "non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent", "non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent", "non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent", "non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent", "non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent", "non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent", "non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent", "non_Hispanic_Black_65_percent",
"non_Hispanic_White_65_percent"), Percent = c(1.56461808677354,
9.89767564517674, 1.59594417033976, 9.9183193848417, 1.64637061018758,
10.1825469990904, 1.69437131145657, 10.3660245814825, 1.74805426417694,
10.5619348870383, 1.80760130953128, 10.7334512832729, 1.86436112939625,
10.9336280959189, 1.92123646192985, 11.1528215113633, 1.97780879616552,
11.3747930080724, 2.03906112613521, 11.6054587640792)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
CodePudding user response:
We don't need the format
here, instead just round
the 'Percent' and paste
with %
RACE<-ggplot(total_RACE_Reshape, aes(x = YEAR, y = Percent, fill = Race))
geom_col()
geom_text(aes(y= Percent,label = paste0(round(Percent, 2), "%")), color= "white", position= position_stack(), hjust=1)
scale_fill_manual(values =c( "#F76900", "#000E54"))
labs(
x = "Year",
y = "Population 65 (%)",
caption = (""),
face = "bold"
)
scale_x_continuous(breaks = unique(total_RACE_Reshape$YEAR))
theme_classic()
coord_flip()