This is my data:
structure(list(aasta = c(2016, 2017, 2018, 2019, 2020, 2016,
2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017,
2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018,
2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019,
2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020,
2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016,
2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017,
2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018,
2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019,
2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020,
2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016,
2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017,
2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018,
2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019,
2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020,
2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016,
2017, 2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017,
2018, 2019, 2020, 2016, 2017, 2018, 2019, 2020, 2016, 2017, 2018,
2019, 2020, 2016, 2017, 2018, 2019, 2020), sihtgrupp = 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,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 9L, 9L, 9L,
10L, 10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L,
8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L), .Label = c("Juhid",
"Tippspetsialistid", "Tehnikud ja keskastme spetsialistid", "Kontoritöötajad/ametnikud",
"Teenindus- ja müügitöötajad", "Põllumajandustöötajad",
"Oskus-ja käsitöölised", "Seadme- ja masinaoperaatorid", "Lihttöölised",
"Sõjaväelased"), class = "factor"), protsent = c(0.0891, 0.0455,
0.0432, 0.0387, 0.038, 0.1058, 0.1219, 0.1123, 0.0988, 0.0991,
0.0634, 0.0848, 0.0792, 0.0735, 0.0742, 0.0689, 0.0594, 0.0613,
0.0613, 0.064, 0.1997, 0.2098, 0.2188, 0.2252, 0.2313, 0.0302,
0.0178, 0.0167, 0.016, 0.0153, 0.2129, 0.1636, 0.1519, 0.1513,
0.1483, 0.1275, 0.1236, 0.1272, 0.1231, 0.1207, 0.1025, 0.1736,
0.1894, 0.2121, 0.2091, NA, NA, NA, NA, NA, 0.0838, 0.0606, 0.0549,
0.0394, 0.0376, 0.1545, 0.1448, 0.1277, 0.1026, 0.0984, 0.1472,
0.0749, 0.0757, 0.0696, 0.0681, 0.0731, 0.0834, 0.0726, 0.0684,
0.0674, 0.1364, 0.1778, 0.195, 0.1943, 0.1968, 0, 0.0063, 0.0119,
0.0137, 0.0136, 0.1668, 0.1468, 0.1315, 0.1299, 0.1266, 0.1148,
0.1035, 0.1042, 0.0944, 0.0882, 0.1234, 0.2019, 0.2265, 0.2877,
0.3033, NA, NA, NA, NA, NA, 0.052, 0.0512, 0.0494, 0.0453, 0.0451,
0.1533, 0.1446, 0.1352, 0.1218, 0.1225, 0.0892, 0.0861, 0.0846,
0.0802, 0.0804, 0.0655, 0.0639, 0.0637, 0.0581, 0.056, 0.2032,
0.2052, 0.2039, 0.1883, 0.1773, 0.0137, 0.0141, 0.0148, 0.0154,
0.0124, 0.1385, 0.1383, 0.1374, 0.1459, 0.147, 0.1147, 0.1173,
0.114, 0.1114, 0.1096, 0.1699, 0.1793, 0.197, 0.2336, 0.2497,
NA, NA, NA, NA, NA, 0.118988520012411, 0.121317947160644, 0.124567474048443,
0.105467004319976, 0.10493451111788, 0.203381942289792, 0.200728818706347,
0.208364675793591, 0.214211231938031, 0.222053000304599, 0.118367980142724,
0.134072274521713, 0.137204754024372, 0.145836436764487, 0.153518123667377,
0.0611231771641328, 0.057698147585788, 0.0526553332330375, 0.0546700431997617,
0.0549802010356381, 0.128762022959975, 0.122988156696022, 0.129532119753272,
0.130939967227767, 0.123667377398721, 0.0164443065466956, 0.0133616762830246,
0.0123363923574545, 0.0119171756293758, 0.011727078891258, 0.13574309649395,
0.133161251138779, 0.130133894990221, 0.125428273499181, 0.123210478221139,
0.126124728513807, 0.118888551472821, 0.117195727395818, 0.121853120810368,
0.113767895217789, 0.0868755817561278, 0.0935, 0.0832, 0.0843,
0.0871, 0.00418864412038474, 0.00425144245368964, 0.004814201895592,
0.00536272903321913, 0.00502589095339628)), row.names = c(NA,
-200L), class = c("tbl_df", "tbl", "data.frame"))
This is my theme and colors:
prx_col_palette <- function(){
c("#1C5679",
"#808C5C",
"#BF2124",
"#D88F2E",
"#5F4B3B",
"#BBB592",
"#87240E",
"#7A989A",
"#204035",
"#F4CE4B",
"#805730",
"#D4613E",
"#212F3D",
"#145A32",
"#808080",
"#E74C3C")
}
prx_cols <- prx_col_palette()
prx_theme <- function(){
theme_bw()+
theme(panel.border = element_blank(),
axis.ticks = element_blank(),
panel.grid.minor.x = element_blank(),
axis.title.x = element_text(hjust = 0.98, margin = margin(t=8)),
axis.title.y = element_text(angle = 90, hjust = 0.98, margin=margin(r=8)))
}
My graph:
ggplot(merilen4, aes(y = protsent, x = aasta, fill = amet))+
geom_bar(stat = 'identity', position = position_fill())+
facet_wrap(~sihtgrupp)+
geom_text(aes(label=scales::percent(protsent, accuracy = 1)), position = position_fill(vjust = 0.5), size=3.8, color="White")+
scale_x_continuous(breaks = merilen4$aasta, labels = merilen4$aasta)+
scale_y_continuous(labels = scales::percent_format(accuracy = 1))+
scale_fill_manual(values = prx_cols)+
labs(
fill = 'Ametinimetus')+
prx_theme()+
theme(axis.title.y = element_text(angle = 0),
axis.text.x = element_text(size=11),
axis.text.y = element_text(size=11),
strip.background =element_blank(),
strip.text =element_text(size=11))+
theme(axis.title.x = element_blank())+
theme(axis.title.y = element_blank())+
theme(legend.position="bottom")
This is what it looks like right now:
However, there are two things wrong with it. Firstly the labels are not visible for some of the smaller categories. Is it possible to maybe add a white square background to the labels and make the text black to make them stand out more and not overlap?
Also, why are the x axis labels (years 2016-2020) all wonky and bold?
You can use the
geom_label
andfill
for a white background of your labels and as @Gregor Thomas said in the comments useunique
for thelabels
andbreaks
to fix overprinting, using the following code:Output: