0

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:

enter image description here

2
  • 1
    I'm curious if there's an extension package that helps with this. The two approaches I've seen are to either plot the text below the plot area and use coord_cartesian(clip = "off"), or else to use gt() to make a table and then align that with patchwork. Neither is as simple as the excel option, unfortunately. Commented Jul 17, 2024 at 18:38
  • 2
    For example: stackoverflow.com/questions/62047359/… Commented Jul 17, 2024 at 18:39

1 Answer 1

1

As suggested by another post, you can use paste with sep="\n". Positioning the legend is a bit of trial and error, but should be possible using various legend. theme arguments.

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) +
  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),
                     labels=~paste(., graf4$rmm,
                                      graf4$perc_mif,
                                      sep="\n")) +
  scale_fill_manual(name = "", values = "#375f8c") +  
  scale_color_manual(name = " ", values = "#60cef2") + 
  labs(x = " ") +
  theme_minimal(base_size = 10) + 
  theme(axis.line.y = element_blank(),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        plot.margin=margin(c(0,1,0.5,1), unit="cm"),
        legend.position = "inside",
        legend.position.inside = c(-0.03, -0.06),
        legend.key.height= unit(1, "mm"),
        legend.margin = margin(c(-3,0,-2,0), unit="mm"),
        legend.spacing.y=unit(0, "mm"),
        panel.grid = element_blank())

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.