【发布时间】:2021-12-30 09:53:48
【问题描述】:
【问题讨论】:
【问题讨论】:
一个选项是shadowtext 包,第二个选项是ggrepel:
library(ggplot2)
library(shadowtext)
df <- data.frame(
x = factor(1),
y = factor(1),
label = "78%"
)
ggplot(df, aes(x = x, y = y, label = label)) +
geom_tile(fill = "firebrick") +
geom_shadowtext(color = "black", size = 14 / .pt, fontface = "bold", bg.colour = "white", bg.r = .2) +
theme_void()
library(ggrepel)
ggplot(df, aes(x = x, y = y, label = label)) +
geom_tile(fill = "firebrick") +
geom_text_repel(color = "black", size = 14 / .pt, fontface = "bold", bg.colour = "white", bg.r = .2, force = 0) +
theme_void()
【讨论】:
您可以简单地使用sprintf 在标签下添加下划线。
我使用来自vcd 包的Arthritis 数据集来演示这个示例。
你会这样做:
parseLabel <- sprintf("underline(%s)~\n~%s",
gsub(" ", "~", df$Treatment, fixed = TRUE),
gsub(" ", "~", length(df$Treatment), fixed = TRUE))
p <- ggplot(df, aes(x = Treatment)) +
geom_histogram(stat = "count") +
stat_count(geom = "text", color = "white", size = 3.5,
aes(label = parseLabel), position = position_stack(vjust = 0.5), parse = T)
p + theme_fivethirtyeight()
【讨论】: