r - 如何在 gtable 内部使用解析并保留尾随零

标签 r ggplot2 printf gridextra plotmath

我想向我的 ggplot 添加一个表格。在该表中,我需要向一些字符值添加下标(对于缩尾均值和标准差)。当我指定 parse = TRUE 时可以实现这一点,但结果是我丢失了使用 sprintf 格式化的尾随零。有没有办法使用绘图并在我的表格中保留尾随零?

# 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

最佳答案

可以打印尾随零,确保 5.0 被解释为字符串,而不是数值。 按照给出的建议here ,解决方案基于使用:

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

因此,修改后的代码为:

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

关于r - 如何在 gtable 内部使用解析并保留尾随零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45886289/

相关文章:

r - 计算矩阵中的 nXn - R?

r - 一个输出文件可用于多种回归

r - 当向量有多个 NaN 时,如何在 R 中内插/外推?

r - 如何在 ggplot2 R 中创建 react 规范(按行加入)?

c++ - 我可以使用 printf() 格式的变量吗

C 打印数组的元素

c - printf 是否需要 %zu 说明符?

r - 是否可以创建自渲染 Rmarkdown 文档?

r - 更改ggplot图表的字体大小

r - 等号更改生存对象自动绘图中图例标签的呈现