我有一个文本文档,想要制作一张热图,根据分配给每个单词的数值对单词进行颜色编码。
关于如何做到这一点的任何想法?
数据框将设置为:
phrase value
The 1
black 9
dog 8
jumped 4
quickly 5
when 2
the 1
car 7
started, 8
but... 1
答案 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))
我知道它不太漂亮,但我认为这是一个起点...如果你有第二个协变量沿着轴打印它会更好
答案 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)
我从这里偷了Duncan Murdoch的代码: http://blog.revolutionanalytics.com/2009/01/multicolor-text-in-r.html