0

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

  • 2
    We don't have your data, and we can't see any output, so all anyone could do is guess as to what's going on. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. – camille May 04 '19 at 02:02
  • 1
    You may want to look into manually converting the appropriate columns (not the entire dataframe) in `table_pet` into character strings with `sprintf`. – Z.Lin May 04 '19 at 03:37
  • Hi @camille I already updated the post with an example database. I hope that is easy to follow – navarro152 May 04 '19 at 17:15
  • @z.lin I am not pretty sure how to do that. I tried this with the same result "annotation_custom(tableGrob(t(table), sprintf("%0.2f",table[c(1:6)]), 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)" Thank you for your suggestion. – navarro152 May 04 '19 at 17:19
  • 1
    I think you're confusing some things. This isn't directly a `ggplot` question—it's just how to format numbers the way you want. Look at the docs for `sprintf`: it takes a vector. Based on your error message, you've given it a list. You also only need to use bare column names in `dplyr` functions, e.g. `length(wt)`, not `length(mtcars$wt)` – camille May 04 '19 at 23:19

1 Answers1

0

grid.table leaves the formatting up to you,

d = data.frame(x = "pi", y = pi)
d2 = d %>% mutate_if(is.numeric, ~sprintf("%.3f",.))

grid.table(d2)
  • Thanks a lot, It worked now, and I am having only two digits on my plot, I still using tableGrom, but looking into the option of using grid.table. I am just finding a way to define the location of the table in the plot – navarro152 May 05 '19 at 04:00
  • @navarro152 If this answer is helpful for you, please consider [accepting](https://stackoverflow.com/help/someone-answers) it. If you have trouble changing the table's location in a plot, it would be better to ask that as a separate question, rather than as a follow-up comment here. – Z.Lin May 06 '19 at 07:11