R中的文字情节:有没有办法做文字热图?

时间:2012-01-12 18:13:05

标签: r text heatmap

我有一个文本文档,想要制作一张热图,根据分配给每个单词的数值对单词进行颜色编码。

关于如何做到这一点的任何想法?

数据框将设置为:

phrase   value
The      1
black    9
dog      8
jumped   4
quickly  5
when     2
the      1
car      7
started, 8
but...   1

2 个答案:

答案 0 :(得分:2)

你是说这样的意思吗?

 psych::read.clipboard(header=FALSE)
 head(data)

          X.value X.phrase
 The           1      The
 black         9    black
 dog           8      dog
 jumped        4   jumped
 quickly       5  quickly
 when          2     when

 ggplot(data=data) + geom_text(aes(x=factor(X.phrase), y=X.value, colour=X.value, label=X.phrase))

我知道它不太漂亮,但我认为这是一个起点...如果你有第二个协变量沿着轴打印它会更好

enter image description here

答案 1 :(得分:2)

data<-structure(list(words = c("The", "black", "dog", "jumped", "quickly", 
"when", "the", "car", "started,", "but..."), cols = c(1, 9, 8, 
4, 5, 2, 1, 7, 8, 1)), .Names = c("words", "cols"), row.names = c(NA, 
-10L), class = "data.frame")

plot(0,0,type='n')
widths <- strwidth(data[,1])
spaces <- rep(strwidth(" "), length(widths)-1)
middle <- mean(par("usr")[1:2])
total <- sum(widths) + sum(spaces)
start <- c(0,cumsum(widths[-length(widths)] + spaces))
total <- sum(widths) + sum(spaces)
start <- start + middle - total/2
pos<-cbind(start,1)
colors<-rainbow(9)

text(pos,data[,1],col=colors[data[,2]],adj=0)

enter image description here

我从这里偷了Duncan Murdoch的代码: http://blog.revolutionanalytics.com/2009/01/multicolor-text-in-r.html