I have been trying to find a solution to this problem for a little while now and all the answers don't seem to be quite what I'm looking for.
I'm sure the answer to this is probably simple and I'm overthinking it.
I've been trying to have a table next to a barplot which corresponds to the same observations in the table. However, the table doesn't seem to line up with the size of the plot because it has too much white space or is too small.
Is there a way that I can have the title of the plot and the title of the columns in the table lineup?
data(mtcars)
library(ggplot2)
library(dplyr)
library(grid)
library(gridExtra)
library(cowplot)
data <- mtcars %>% select(mpg, disp, cyl, qsec) %>% tibble::rownames_to_column("Car Name") %>% slice(1:7)
data$`Car Name` <- factor(data$`Car Name`, levels = data$`Car Name`)
t <- tableGrob(data %>% slice(1:7) %>% select(-mpg),
theme = ttheme_minimal(),
rows = NULL)
plot(t)
p <- ggplot(data = data, aes(x = mpg, y = `Car Name`)) +
geom_bar(stat = "identity", fill = "white", color = "black", alpha = 0.3, size = .75) + theme_classic() +
theme(axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_blank(),
plot.title = element_text(face = "bold")) +
ggtitle("No. of mpg") +
scale_x_continuous(expand = expansion(mult = c(0, .1)), limits = c(0,30)) +
scale_y_discrete(limits=rev)
p
grid.arrange(t, p, nrow = 1)
This is what I have done to make the table and plot. I have a basic grid.arrange at the bottom to highlight my issue.
The image here highlights the differences in size between the table and the plot