在同一geom_text

时间:2019-05-26 23:02:18

标签: r ggplot2 graph ggrepel

我有一个图(用ggplot2在R中制成)是一堆文本数据的奇异值分解的结果,所以我基本上有一个约100个单词的数据集,用于某些评论和〜 10个类别的评论,每个评论都有2D坐标。由于文本的数量以及许多要点的紧密程度,我无法使情节看起来清晰易懂。

现在数据的结构方式是,我绘制2个不同的geom_texts,它们具有不同的格式和其他格式,并分别传递一个单独的坐标数据框。这很容易,因为如果〜10个类别与〜100个术语重叠(这是次要的),并且我希望两者的格式完全不同,那很好,但是并不一定有理由不能将它们放在一起数据框和geom我猜是否有人可以找到解决方案。

我想做的是使用ggrepel功能,使〜10个类别相互排斥,并使用shadowtext功能使它们在彩色单词的背景中脱颖而出,但是由于它们是不同的geom,因此我不确定如何实现。

带有伪造数据的最小示例:

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")

第二个情节的结果令人反感: messy plot with a lot of words but the category labels are separated

最后一个绘图的结果,类别标签周围有这些看起来干净的白色缓冲区: messy plot with the category labels all on top of each other but outlined in white

有两种方法都可以吗?我尝试使用geom_label_repel,但没有边框,但我认为它看起来不像阴影文本解决方案那么干净。

0 个答案:

没有答案