在这种情况下,还可以绘制比较云。
为此,我们首先将数据从长格式转换为宽格式:
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"))
data
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))