2017-08-25 4 views
2

Je souhaite ajouter une table à mon ggplot. Dans ce tableau, j'ai besoin d'ajouter un indice à quelques valeurs de caractères (pour les moyennes et les écarts-types). Cela peut être réalisé lorsque je spécifie parse = TRUE mais en conséquence, je perds les zéros de fin formatés en utilisant sprintf. Y at-il un moyen d'utiliser plotmath et de garder des zéros à la fin de ma table?Comment utiliser l'analyse à l'intérieur d'un gtable et conserver les zéros de fin

# exmple data 
data(iris) 

# packages 
library(dplyr) 
library(tidyr) 
library(ggplot2) 
library(psych) 
library(gridExtra) 
library(grid) 
library(gtable) 

# sumarise data 
summary.df <- iris %>% 
    group_by(Species) %>% 
    summarise(mean_length = mean(Sepal.Length), 
      meanw_length = winsor.mean(Sepal.Length), 
      sd_length = sd(Sepal.Length), 
      sdw_length = winsor.sd(Sepal.Length, trim=0.05)) %>% 
    gather(key='Metric', value='Value', 
     mean_length, meanw_length, 
     sd_length, sdw_length) %>% 
    mutate(Value = sprintf("%2.1f", Value)) %>% 
    spread(key=Species, value=Value) 

# rename metrics 
# use plotmath notation for subsript 
summary.df$Metric[summary.df$Metric == 'mean_length'] <- 'Mean' 
summary.df$Metric[summary.df$Metric == 'meanw_length'] <- 'Mean[w]' 
summary.df$Metric[summary.df$Metric == 'sd_length'] <- 'SD' 
summary.df$Metric[summary.df$Metric == 'sdw_length'] <- 'SD[w]' 

# regular theme 
tt <- ttheme_default(core = list(fg_params=list(cex = 0.7)), 
        colhead = list(fg_params=list(cex = 0.7)), 
        rowhead = list(fg_params=list(cex = 0.7))) 

# theme with parsing 
tt_parse <- ttheme_default(core = list(fg_params=list(cex = 0.7, 
                 parse=TRUE)), 
          colhead = list(fg_params=list(cex = 0.7)), 
          rowhead = list(fg_params=list(cex = 0.7))) 


# Graph with regular table theme 
iris %>% 
    ggplot(aes(x=Sepal.Length, fill=Species)) + 
    geom_density(alpha = 0.8) + 
    coord_cartesian(ylim = c(0, 1.5)) + 
    labs(title = 'Regular Theme') + 
    annotation_custom(grob=tableGrob(summary.df, theme=tt, rows=NULL), 
        xmin=6.25, xmax=8, 
        ymin = 1, ymax=1.4) 

# graph with table theme with parsing 
iris %>% 
    ggplot(aes(x=Sepal.Length, fill=Species)) + 
    geom_density(alpha = 0.8) + 
    coord_cartesian(ylim = c(0, 1.5)) + 
    labs(title = 'Theme with Parsing') + 
    annotation_custom(grob=tableGrob(summary.df, theme=tt_parse, rows=NULL), 
        xmin=6.25, xmax=8, 
        ymin = 1, ymax=1.4) 

Regular Plot Table

Plot Table with Parsing

Répondre

2

zéros en fin de chaîne peuvent être imprimés en sorte que 5,0 est interprété comme une chaîne de caractères, et non pas une valeur numérique. Suite aux suggestions données here, la solution est basée sur l'utilisation de:

sprintf('"%2.1f"',5.0) 
# [1] "\"5.0\"" 

Par conséquent, le code modifié est:

data(iris) 
library(dplyr) 
library(tidyr) 
library(ggplot2) 
library(psych) 
library(gridExtra) 
library(grid) 
library(gtable) 

summary.df <- iris %>% 
    group_by(Species) %>% 
    summarise(mean_length = mean(Sepal.Length), 
      meanw_length = winsor.mean(Sepal.Length), 
      sd_length = sd(Sepal.Length), 
      sdw_length = winsor.sd(Sepal.Length, trim=0.05)) %>% 
    gather(key='Metric', value='Value', 
     mean_length, meanw_length, 
     sd_length, sdw_length) %>% 
    mutate(Value = sprintf('"%2.1f"', Value)) %>% 
    spread(key=Species, value=Value) 

summary.df$Metric[summary.df$Metric == 'mean_length'] <- 'Mean' 
summary.df$Metric[summary.df$Metric == 'meanw_length'] <- 'Mean[w]' 
summary.df$Metric[summary.df$Metric == 'sd_length'] <- 'SD' 
summary.df$Metric[summary.df$Metric == 'sdw_length'] <- 'SD[w]' 

tt_parse <- ttheme_default(core = list(fg_params=list(cex = 0.7, 
                 parse=TRUE)), 
          colhead = list(fg_params=list(cex = 0.7)), 
          rowhead = list(fg_params=list(cex = 0.7))) 

iris %>% 
    ggplot(aes(x=Sepal.Length, fill=Species)) + 
    geom_density(alpha = 0.8) + 
    coord_cartesian(ylim = c(0, 1.5)) + 
    labs(title = 'Theme with Parsing') + 
    annotation_custom(grob=tableGrob(summary.df, theme=tt_parse, rows=NULL), 
        xmin=6.25, xmax=8, 
        ymin = 1, ymax=1.4) 

enter image description here