R中的Wordcloud:基于其他列中的数据的颜色

时间:2019-01-05 09:38:41

标签: r

我正在尝试获取一个wordcloud,其中wordcloud的颜色基于数据框中的另一列。因此,我正在使用wordcloud2和RColorBrewer软件包。我正在使用以下(示例)代码:

set.seed(1) 

DF <- data.frame(
  word = c('football','tennis','squash','curling','baseball','diving','archery','cricket','cycling','hockey','formula1','rugby','volleyball','tabletennis','swimming','shooting','taekwondo','judo','handball','horseracing'), 
  freq = sample(100:1000,20), 
  diff = sample(-100:100,20)/100)

library(wordcloud2)
library(RColorBrewer)

color_range_number <- length(unique(DF$diff))
custColorPal <- colorRampPalette(c("#ff0000","#00cc00"))
custColors <- custColorPal(color_range_number)
colors <- custColors[factor(DF$diff)]

wordcloud2(data = DF, color = colors)

DF如下:

          word freq  diff
1     football  339  0.87
2       tennis  434 -0.58
3       squash  614  0.29
4      curling  915 -0.76
5     baseball  280 -0.48
6       diving  904 -0.25
7      archery  945 -0.98
8      cricket  690 -0.26
9      cycling  661  0.67
10      hockey  155 -0.35
11    formula1  283 -0.08
12       rugby  257  0.13
13  volleyball  710 -0.07
14 tabletennis  441 -0.65
15    swimming  782  0.54
16    shooting  540  0.24
17   taekwondo  735  0.46
18        judo  976 -0.81
19    handball  435  0.32
20 horseracing  785  0.93

在DF中,我想使用“ diff”列为单词分配颜色:负值越大,红色越多,正值越大,绿色越多。

但是,我得到了意想不到的结果,例如'曲棍球'被涂成绿色,而由于变量'diff'的diff值为-0.35,因此它应该具有更多的红色。另请参见this screenshot

我认为这是由于没有绘制所有单词,因为例如没有绘制“ horseracing”。

我的问题:

  1. 由于并非所有单词都被绘制出来而声明颜色“分配错误”是否正确?
  2. 如何确保始终绘制所有单词?减小参数“ size”的值并不总是保证。可能要注意,我想通过rmarkdown将这个wordcloud粘贴到PDF中。

1 个答案:

答案 0 :(得分:0)

class myService { private $accessGranted; public function __construct(Security $security) { // user might not be set up yet? $accessGranted = $security->isGranted('ROLE_ADMIN'); } public function someFunctionWithSecurity() { if( $accessGranted ) // do the admin stuff here else // do slightly different stuff here } } 行引起了我认为的问题。试试这个...

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.https.onRequest((request, response) => {
    const payLoad = {
        data: {
            execute_noise_measure: "true"
        }
    };

    const options = {
        priority: "high"
    };

    return admin.messaging().sendToTopic("all", payLoad, options).then(function(response) {
            return console.log("Successfully sent message:", response);
        })
        .catch(function(error) {
            return console.log("Error sending message:", error);
        });
});

所有custColorPal函数所做的只是查找diff值以在颜色范围内缩放它。因子被解释为等距的数字,范围为1到20。

关于第二个问题,我的建议是使字体变小,以便更有可能显示所有单词