I want to add a summary table to plot with ggplot. I am using annotation_custom to add a previous created table. My problem is that the table shows a different number of decimals. As an example I am using the mtcars database and my lines of code are the following:
rm(list=ls()) #Clear environment console
data(mtcars)
head(mtcars)
library(dplyr)
library(tidyr)
library(ggplot2)
library(gridExtra)
table <- mtcars %>% #summary table that needs to be avelayed to the plot
  select(wt) %>%
  summarise(
    Obs = length(mtcars$wt),
    Q05 = quantile(mtcars$wt, prob = 0.05),
    Mean = mean(mtcars$wt),
    Med = median(mtcars$wt),
    Q95 = quantile(mtcars$wt, prob = 0.95),
    SD = sd(mtcars$wt)) 
dens <- ggplot(mtcars) + #Create example density plot for wt variable
  geom_density(data = mtcars, aes(mtcars$wt))+
  labs(title = "Density plot")
plot(dens)
dens1 <- dens + #Overlaping summary table to density plot
  annotation_custom(tableGrob(t(table), 
                              cols = c("WT"),
                              rows=c("Obs", "Q-05", "Mean", "Median", "Q-95", "S.D." ),
                              theme = ttheme_default(base_size = 11)),
                    xmin=4.5, xmax=5, ymin=0.2, ymax=0.5)
print(dens1)
Running the previous I obtain the following picture density plot
I would like to fix the number of displayed decimals to only 2.
I already tried adding sprintf
annotation_custom(tableGrob(t(sprintf("%0.2f",table)),
But obtained the following error "Error in sprintf("%0.2f", table_pet) : (list) object cannot be coerced to type 'double'"
I have been looking without any look. Any idea how can I do this.
Thank you in advance
 
    