在同一个 geom_text 上使用 ggrepel 和 shadowtext
Using ggrepel and shadowtext on the same geom_text
我有一个图(用 ggplot2
在 R 中制作),它是一堆文本数据的一些奇异值分解的结果,所以我基本上有一些评论中使用的约 100 个单词的数据集以及大约 10 类评论,每类评论都有 2D 坐标。由于文本的数量以及许多重要点的紧密程度,我很难让情节看起来清晰。
我的数据现在的结构方式是,我正在绘制 2 个不同的 geom_texts
,格式不同等等,将每个数据传递给一个单独的坐标数据框。这更容易,因为如果 ~10 个类别与 ~100 个术语重叠(这是次要的)并且我希望两者的格式完全不同,但不一定有理由不能将它们放在一起数据框和 geom
我猜是否有人能想出解决方案。
我想做的是使用 ggrepel
功能使 ~10 个类别相互排斥,并使用 shadowtext
功能使它们从彩色背景中脱颖而出单词,但由于它们不同 geom
s 我不确定如何实现。
带有一些假数据的最小示例:
library(ggplo2)
library(ggrepel)
library(shadowtext)
dictionary <- c("spicy", "Thanksgiving", "carborator", "mixed", "cocktail", "stubborn",
"apple", "rancid", "table", "antiseptic", "sewing", "coffee", "tragic",
"nonsense", "stufing", "words", "bottle", "distillery", "green")
tibble(Dim1 = rnorm(100),
Dim2 = rnorm(100),
Term = sample(dictionary, 100, replace = TRUE),
Color = as.factor(sample.int(10, 100, replace = TRUE))) -> words
tibble(Dim1 = c(-1,-1,0,-0.5,0.25,0.25,0.3),
Dim2 = c(-1,-0.9, 0, 0, 0.25, 0.4, 0.1),
Term = c("Scotland", "Ireland", "America", "Taiwan", "Japan", "China", "New Zealand")) -> locations
#Base graph
ggplot() +
xlab("Factor 1") +
ylab("Factor 2") +
theme(legend.position = "none") +
geom_text_repel(aes(x = Dim1, y = Dim2, label = Term, color = Color),
words,
fontface = "italic", size = 8) -> p
#Cluttered and impossible to read:
p + geom_text(aes(x = Dim1, y = Dim2, label = Term),
locations,
fontface = "bold", size = 16, color = "#747474")
#I can make it repel:
p + geom_text_repel(aes(x = Dim1, y = Dim2, label = Term),
locations,
fontface = "bold", size = 16, color = "#747474")
#Or I can make the shadowtext:
p + geom_shadowtext(aes(x = Dim1, y = Dim2, label = Term),
locations,
fontface = "bold", size = 16, color = "#747474", bg.color = "white")
第二个情节的结果,很好地排斥:
最后一张图的结果,类别标签周围有这些看起来很干净的白色缓冲区:
有没有办法做到这两点?我尝试使用无边框的 geom_label_repel
,但我认为它看起来不如 shadowtext 解决方案干净。
这个答案来得有点晚,但我最近发现自己陷入了类似的困境,并想出了一个解决方案。我正在写,因为它可能对其他人有用。
#I can make it repel:
p + geom_text_repel(aes(x = Dim1, y = Dim2, label = Term),
locations,
fontface = "bold", size = 16,
color = "white",
bg.color = "black",
bg.r = .15)
geom_text_repel 中的 bg.color
和 bg.r
选项允许您 select 文本的底纹颜色和大小,显着提高图像的对比度(见下文!)。此解决方案借鉴自
我有一个图(用 ggplot2
在 R 中制作),它是一堆文本数据的一些奇异值分解的结果,所以我基本上有一些评论中使用的约 100 个单词的数据集以及大约 10 类评论,每类评论都有 2D 坐标。由于文本的数量以及许多重要点的紧密程度,我很难让情节看起来清晰。
我的数据现在的结构方式是,我正在绘制 2 个不同的 geom_texts
,格式不同等等,将每个数据传递给一个单独的坐标数据框。这更容易,因为如果 ~10 个类别与 ~100 个术语重叠(这是次要的)并且我希望两者的格式完全不同,但不一定有理由不能将它们放在一起数据框和 geom
我猜是否有人能想出解决方案。
我想做的是使用 ggrepel
功能使 ~10 个类别相互排斥,并使用 shadowtext
功能使它们从彩色背景中脱颖而出单词,但由于它们不同 geom
s 我不确定如何实现。
带有一些假数据的最小示例:
library(ggplo2)
library(ggrepel)
library(shadowtext)
dictionary <- c("spicy", "Thanksgiving", "carborator", "mixed", "cocktail", "stubborn",
"apple", "rancid", "table", "antiseptic", "sewing", "coffee", "tragic",
"nonsense", "stufing", "words", "bottle", "distillery", "green")
tibble(Dim1 = rnorm(100),
Dim2 = rnorm(100),
Term = sample(dictionary, 100, replace = TRUE),
Color = as.factor(sample.int(10, 100, replace = TRUE))) -> words
tibble(Dim1 = c(-1,-1,0,-0.5,0.25,0.25,0.3),
Dim2 = c(-1,-0.9, 0, 0, 0.25, 0.4, 0.1),
Term = c("Scotland", "Ireland", "America", "Taiwan", "Japan", "China", "New Zealand")) -> locations
#Base graph
ggplot() +
xlab("Factor 1") +
ylab("Factor 2") +
theme(legend.position = "none") +
geom_text_repel(aes(x = Dim1, y = Dim2, label = Term, color = Color),
words,
fontface = "italic", size = 8) -> p
#Cluttered and impossible to read:
p + geom_text(aes(x = Dim1, y = Dim2, label = Term),
locations,
fontface = "bold", size = 16, color = "#747474")
#I can make it repel:
p + geom_text_repel(aes(x = Dim1, y = Dim2, label = Term),
locations,
fontface = "bold", size = 16, color = "#747474")
#Or I can make the shadowtext:
p + geom_shadowtext(aes(x = Dim1, y = Dim2, label = Term),
locations,
fontface = "bold", size = 16, color = "#747474", bg.color = "white")
第二个情节的结果,很好地排斥:
最后一张图的结果,类别标签周围有这些看起来很干净的白色缓冲区:
有没有办法做到这两点?我尝试使用无边框的 geom_label_repel
,但我认为它看起来不如 shadowtext 解决方案干净。
这个答案来得有点晚,但我最近发现自己陷入了类似的困境,并想出了一个解决方案。我正在写,因为它可能对其他人有用。
#I can make it repel:
p + geom_text_repel(aes(x = Dim1, y = Dim2, label = Term),
locations,
fontface = "bold", size = 16,
color = "white",
bg.color = "black",
bg.r = .15)
geom_text_repel 中的 bg.color
和 bg.r
选项允许您 select 文本的底纹颜色和大小,显着提高图像的对比度(见下文!)。此解决方案借鉴自