I'm using R to make a combined barplot + line with ggplot2. I want to add a data table in the x axis just like it's possible to do in excel.
This is my data and the plot I managed to make:
graf4 <- data.frame(anoobito = c(2006:2023),
perc_mif = c(37.5, 40.9, 65.3, 76.4, 84.4, 85.0, 86.0, 89.1, 95.1, 92.6, 93.8, 95.8, 95.0, 94.6, 92.4, 91.6, 95.8, 98.0),
rmm = c(52.4, 69.5, 60.7, 72.3, 74.5, 69.8, 63.7, 75.5, 71.2, 74.0, 74.6, 82.9, 60.6, 81.0, 114.0, 156.0, 77.4, 70.3))
ggplot(data = graf4, aes(x = anoobito)) +
geom_bar(aes(y = rmm, fill = "RMM"), stat = "identity", width = 0.8) +
geom_line(aes(y = perc_mif * (160 / 100), color = "% Inv. MIF"), size = 1) + # Color for line
geom_text(aes(y = rmm, label = rmm), color = "white", vjust = 8, hjust = 0.5, size = 6) + # Labels for rmm inside bars
geom_text(aes(y = perc_mif * (160 / 100), label = perc_mif), color = "black", vjust = -0.5, size = 6) + # Labels for perc_mif
scale_y_continuous(
expand = expansion(mult = c(0, 0.1)),
name = "RMM/100.00 NV",
limits = c(0, 160),
breaks = seq(0, 160, by = 20),
sec.axis = sec_axis(~ . * (100 / 160), name = "% investigação MIF", breaks = seq(0, 100, by = 10))
) +
scale_x_continuous(breaks = seq(2006, 2023, by = 1)) +
scale_fill_manual(name = " ", values = "#375f8c") + # Legend for bars
scale_color_manual(name = " ", values = "#60cef2") + # Legend for line
labs(x = " ") +
theme_minimal(base_size = 20) +
theme(axis.line.y = element_blank(),
axis.text.x = element_text(colour = "black"),
axis.text.y = element_text(colour = "black"),
legend.position = "bottom",
panel.grid = element_blank())
This is the desired output:


coord_cartesian(clip = "off"), or else to usegt()to make a table and then align that withpatchwork. Neither is as simple as the excel option, unfortunately.