图上的粗体字

时间:2018-12-18 00:36:59

标签: r ggplot2

我正在用摘要统计信息注释我的图表。我想使用粗体字体快速按组将用户的眼睛吸引到最佳/最差统计信息。突出显示的数字将需要在运行时由数据本身确定。

这是一个使用ChickWeight数据集的示例,显示了根据雏鸡的饮食而引起的雏鸡体重的变化:

library(ggplot2)
library(dplyr)

# Calculate end vs start weights
df <- merge(filter(ChickWeight, Time==21), filter(ChickWeight, Time==0), by=c("Chick", "Diet"))
df$dWeight <- df$weight.x - df$weight.y

# Summary statistics: sd & mean
df.stat <- do.call(data.frame, 
                   aggregate(dWeight ~ Diet, 
                             data=df, 
                             FUN = function(x) c(SD=sd(x), MN=mean(x))))

ggplot(data = df) + 
    facet_grid(Diet ~ .) +
    geom_histogram(binwidth=10, aes(x=dWeight)) + 
    geom_vline(data=df.stat, aes(xintercept = dWeight.MN), color="black") + 
    geom_text(data=df.stat, aes(x=Inf, 
                                y=Inf, 
                                label = sprintf("\nmean = %4.1f\nsd = %4.1f", 
                                                dWeight.MN, dWeight.SD), 
                                hjust=1, 
                                vjust=1)) 

在下图中,我只想突出显示以下文本:
在第3组中,“平均值= 229.5”将变为“平均值= 229.5
在第4组中,“ sd = 43.9”将变为“ sd = 43.9

enter image description here

2 个答案:

答案 0 :(得分:2)

1

如果您不想muck around with parsing,则可以将条件添加到绘图标签中,这样您将非常接近。

数据

df.plot <- df %>%
    # Combine df and df.stat -
    # this also removes the calls to df.stat in your secondary geoms.
    left_join(df.stat, by = "Diet") %>%
    # Add global maximum of MN and global minimum of SD to every row.
    mutate(dWeight.MN.max = max(dWeight.MN),
           dWeight.SD.min = min(dWeight.SD))

代码

ggplot(data = df.plot) + 
    facet_grid(Diet ~ .) +
    geom_histogram(binwidth = 10, aes(x = dWeight)) + 
    geom_vline(aes(xintercept = dWeight.MN), color="black") + 
    geom_text(aes(x = Inf, 
                  y = Inf, 
                  label = sprintf("\nmean = %4.1f", dWeight.MN), 
                  hjust = 1,
                  vjust = 1,
                  # bold if mean == mean maximum
                  fontface = ifelse(dWeight.MN == dWeight.MN.max, 2, 1))) +
    geom_text(aes(x = Inf, 
                  y = Inf, 
                  label = sprintf("\n\nsd = %4.1f", dWeight.SD), 
                  hjust = 1,
                  vjust = 1,
                  # bold if sd == sd minimum
                  fontface = ifelse(dWeight.SD == dWeight.SD.min, 2, 1))) +
    theme_gray()

说明

通过fontface =,您可以将geom_text()设置为斜体粗体。 表达式中的ifelse()检查值是否等于全局最大值/最小值,如果为true,则将文本设置为粗体(= 2),如果为false,则将文本保留为纯文本(= 1)。

答案 1 :(得分:0)

Selective, partially bold text with facet_grid

采用@Roman的ifelse想法,这是一个使用latex2exp库构建LaTeX字符串的解决方案,该字符串允许在字符串内 bold 更改字体。 latex2exp将TeX字符串转换为绘图表达式。

仍然不够完美,无法扩展到两行以上的文字。 latex2exp does not appear to support newlines,迫使我改用overset

另一个LaTeX选项将是{nx 1}矩阵,但是latex2exp也不支持矩阵(运行latex2exp_supported()来查看支持的LaTeX表达式 )。

如果在用户调整图的大小或缩放时存在可靠的间距和对齐文本的方式,则可以使用两个单独的geom_text命令。

此解决方案仅限于只能彼此居中对齐的2条线。

数据

library(ggplot2)
library(dplyr)
library(latex2exp)

# Calculate end - start weights
df <- inner_join(filter(ChickWeight, Time==21), 
                 filter(ChickWeight, Time==0), 
                 by=c("Chick", "Diet")) %>%
      mutate(dWeight=weight.x-weight.y) %>% 
      select(Chick, Diet, dWeight)

# Summary statistics: sd & mean
df.stats <- df %>% 
            group_by(Diet) %>% 
            summarise(MN=mean(dWeight), SD=sd(dWeight)) %>% 
            mutate(is.max.MN=(MN==max(MN))) %>% 
            mutate(is.min.SD=(SD==min(SD)))

ggplot命令

ggplot(data=df) + 
    facet_grid(Diet ~ .) +
    geom_histogram(binwidth=10, aes(x=dWeight)) + 
    geom_vline(data=df.stats, aes(xintercept = MN), color="black") + 
    geom_text(data=df.stats,
              aes(x=Inf, 
                  y=Inf, 
                  hjust=1, 
                  vjust=1),
              label = TeX(paste("\\overset{mean =", 
                                sprintf(ifelse(df.stats$is.max.MN, "\\textbf{%4.1f}", "%4.1f"), df.stats$MN),
                                "}{sd =",
                                sprintf(ifelse(df.stats$is.min.SD, "\\textbf{%4.1f} $", "%4.1f"), df.stats$SD),
                                "}"
                          )))

label的{​​{1}}在geom_text函数外部,该函数似乎不会继承数据名称空间。

此外,此ggplot命令还会生成警告消息(aes语句):
在is.na(x)中:is.na()应用于类型为“表达式”的非(列表或向量)