r - 带渐变色/透明字的GGWordcloud(GGPlot Wordcloudgradient with adjustmentcolor)

标签 r ggplot2 colors word-cloud quanteda

我使用ggwordcloud创建了一个词云,因为不幸的是我无法使用替代的词云包。到目前为止,我能够根据我的要求自定义 ggwordcloud,但遗憾的是我错过了淡入透明的渐变的实现。到目前为止我还没有找到允许这样做的函数。

下面的代码创建了wordcloud,但只有两种颜色,但我需要一个渐变,它越来越透明(如第二个代码示例中所示),以便最小的单词是隐藏/透明的。

library(ggwordcloud)
data("love_words_small")
data("love_words")

set.seed(42)
ggplot(
  love_words_small,
  aes(
    label = word, size = speakers,
    color = speakers
  )
) +
  geom_text_wordcloud_area() +
  scale_size_area(max_size = 24) +
  theme_minimal() +
  scale_color_gradient(low = "darkred", high = "red")

通过 quanteda Wordcloud 包实现的以下实现已经使用 adjustcolor 解决了我迄今为止的问题:

library(quanteda)
library(quanteda.textplots)
set.seed(10)
dfmat1 <- dfm(corpus_subset(data_corpus_inaugural, President == "Obama"),
              remove = stopwords("english"), remove_punct = TRUE) %>%
   dfm_trim(min_termfreq = 3)
col <- sapply(seq(0.1, 1, 0.1), function(x) adjustcolor("#1F78B4", x))

textplot_wordcloud(dfmat1, adjust = 0.5, random_order = FALSE, 
                   color = col, rotation = FALSE)

有什么办法可以将此解决方案转移到ggwordcloud吗?

enter image description here

非常感谢您的建议!

最佳答案

我自己找到了解决方案。太明显了...

col <- sapply(seq(0.1, 1, 0.1), function(x) adjustcolor("#1F78B4", x))

library(ggwordcloud)
data("love_words_small")
data("love_words")

set.seed(42)
ggplot(
  love_words_small,
  aes(
    label = word, size = speakers,
    color = speakers
  )
) +
  geom_text_wordcloud_area() +
  scale_size_area(max_size = 24) +
  theme_minimal() +
  scale_color_gradientn(colours = col)

关于r - 带渐变色/透明字的GGWordcloud(GGPlot Wordcloudgradient with adjustmentcolor),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68297017/

相关文章:

r - 使用 dplyr group_by 时将汇总条件应用于一系列列?

r - 在 R Week 中将每日数据转换为每周数据从周六开始

R:PDF格式的位图输出

r - ggplot2 中多个图例的不同方向?

r - ggplot 来自同一数据帧的多线图

CSS 和 .less : Set a font color if user pick a specific color through web interface

c++ - 从高度编码的 RGB 值

R不会调用gfortran编译对象?

r - 在 ggarrange() 中设置高图表格的宽度 - 可能的错误? (R,ggplot,鸡蛋)

java - 相交形成闭合形状的多条绘图线的填充颜色