R中的字云有两个单独的值

时间:2016-03-16 23:35:03

标签: r word-cloud

作为R的新手,我正在制作一个显示两个变量的文字云:frequencyrating。使用通用表格,我希望按州和假设的平均大学评级显示假设的大学数量(字体=大到小)

  • 1 =绿色(好),
  • 3 =黄色(平均值),
  • 5 =红色(差)

我能够创建描述fonts =大学数量的云,但不能将评级与第三列联系起来。这是我的通用表:

State   Colleges    Rating
Alabama        220      1
Alaska         100      3
Arizona         50      5
Arkansas       275      1
California     155      3
Colorado        68      5
Connecticut    235      1
Delaware       189      3
Florida         32      5
Georgia        219      1
Hawaii         117      3
Idaho           63      5
Illinois       264      1
Indiana        167      3
Iowa            76      5
Kansas         287      1
Kentucky       178      3
Louisiana       67      5
Maine          246      1
Maryland       169      3
Massachusetts   46      5
Michigan       225      1
Minnesota      132      3
Mississippi     23      5
Missouri       219      1
Montana        194      3
Nebraska        97      5

以下是我非常简单的脚本:

library(wordcloud)
library(rcolorbrewer)

data <- read.csv("wordcloud.csv", header = T)
pal <- brewer.pal(9, "RdYlGn")
wordcloud(data$State, data$Colleges, scale = c(4,1), colors = pal, rot.per=.5)

上面的脚本允许文本大小反映大学的数量,但是我无法将1 =绿色(好)的颜色渐变链接到3 =黄色(平均值)到5 =红色(坏)。非常感谢任何建议。

2 个答案:

答案 0 :(得分:3)

在这种情况下,还可以绘制比较云。

为此,我们首先将数据从长格式转换为宽格式:

library(reshape2)
df1 <- dcast(df1,State + Colleges ~ Rating, value.var = "Colleges")

然后我们执行一些标准操作来准备合适的矩阵:

rownames(df1) <- df1[,1] #use name of States as row names
df1 <- df1[,-c(1,2)] #remove "States" and "Colleges" column
df1[is.na(df1)] <- 0  #set NA values to zero
df1 <- as.matrix(df1) #convert into matrix
colnames(df1) <- c("good", "average", "bad")

最后,我们可以绘制比较云并根据需要为组指定颜色:

library(wordcloud)
comparison.cloud(df1,max.words=Inf,random.order=FALSE, scale = c(4,.5), 
                     title.size = 1,  colors=c("green","orange","red"))

enter image description here

数据

df1 <- structure(list(State = structure(1:27, .Label = c("Alabama", 
"Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", 
"Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", 
"Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", 
"Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", 
"Missouri", "Montana", "Nebraska"), class = "factor"), Colleges = c(220L, 
100L, 50L, 275L, 155L, 68L, 235L, 189L, 32L, 219L, 117L, 63L, 
264L, 167L, 76L, 287L, 178L, 67L, 246L, 169L, 46L, 225L, 132L, 
23L, 219L, 194L, 97L), Rating = c(1L, 3L, 5L, 1L, 3L, 5L, 1L, 
3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 
5L, 1L, 3L, 5L)), .Names = c("State", "Colleges", "Rating"), 
class = "data.frame", row.names = c(NA, -27L))

答案 1 :(得分:2)

您可以手动分配颜色并添加public static String getMd5Key(String password) { // String password = "12131123984335"; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(password.getBytes()); byte byteData[] = md.digest(); //convert the byte to hex format method 1 StringBuffer sb = new StringBuffer(); for (int i = 0; i < byteData.length; i++) { sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); } System.out.println("Digest(in hex format):: " + sb.toString()); //convert the byte to hex format method 2 StringBuffer hexString = new StringBuffer(); for (int i = 0; i < byteData.length; i++) { String hex = Integer.toHexString(0xff & byteData[i]); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } System.out.println("Digest(in hex format):: " + hexString.toString()); return hexString.toString(); } catch (Exception e) { // TODO: handle exception } return ""; }

ordered.colors=T

enter image description here