R 中的文本挖掘,术语图与值的相关性
text mining in R, correlation of terms plot with the values
我画了一个关于文本挖掘中术语相关性的图。
我想把相关值放在像下面的图像一样的线旁边。
我应该在 plot() 旁边添加什么?文本()?或者还有其他选择吗?
R代码;项的相关性
freq.terms<-findFreqTerms(dtm, lowfreq=500)[1:25]
plot(dtm,term=freq.terms,corThreshold=0.25,weighting=T)
这就是我所在的位置。主要思想是制作一个边属性列表,我们可以将其传递给 plot
.
library(tm)
library(graph)
library(igraph)
# Install Rgraphviz
source("http://bioconductor.org/biocLite.R")
biocLite("Rgraphviz")
data("acq")
dtm <- DocumentTermMatrix(acq,
control = list(weighting = function(x) weightTfIdf(x, normalize=FALSE),
stopwords = TRUE))
freq.terms <- findFreqTerms(dtm, lowfreq=10)[1:25]
assocs <- findAssocs(dtm, term=freq.terms, corlimit=0.25)
# Recreate edges, using code from plot.DocumentTermMatrix
m <- dtm
corThreshold <- 0.25
m <- as.matrix(m[, freq.terms])
c <- cor(m)
c[c < corThreshold] <- 0
c[is.na(c)] <- 0
diag(c) <- 0
ig <- graph.adjacency(c, mode="undirected", weighted=TRUE)
g1 <- as_graphnel(ig)
# Make edge labels
ew <- as.character(unlist(edgeWeights(g1)))
ew <- ew[setdiff(seq(along=ew), Rgraphviz::removedEdges(g1))]
names(ew) <- edgeNames(g1)
eAttrs <- list()
elabs <- paste(" ", round(as.numeric(ew), 2)) # so it doesn't print on top of the edge
names(elabs) <- names(ew)
eAttrs$label <- elabs
fontsizes <- rep(7, length(elabs))
names(fontsizes) <- names(ew)
eAttrs$fontsize <- fontsizes
plot(dtm, term=freq.terms, corThreshold=0.25, weighting=T,
edgeAttrs=eAttrs)
剩下的主要问题是绘图打印了两次边缘标签:一次使用默认设置,显然,另一次使用我们在 eAttrs
中指定的字体大小。
Edit. 看来为了让标签正确渲染,我们不能直接使用plot
。使用 renderGraph
(plot
调用)似乎有效。我们为边缘权重创建一个数值向量,并将其作为 lwd
参数传递给 renderEdgeInfo
。您必须更改标签的手动偏移量 (elabs <- paste(" ",...)
),以便标签与边缘的距离正确。
weights <- as.numeric(ew)
names(weights) <- names(ew)
edgeRenderInfo(g1) <- list(label=elabs, fontsize=fontsizes, lwd=weights*5)
nodeRenderInfo(g1) <- list(shape="box", fontsize=20)
g1 <- layoutGraph(g1)
renderGraph(g1)
我画了一个关于文本挖掘中术语相关性的图。
我想把相关值放在像下面的图像一样的线旁边。
我应该在 plot() 旁边添加什么?文本()?或者还有其他选择吗?
R代码;项的相关性
freq.terms<-findFreqTerms(dtm, lowfreq=500)[1:25]
plot(dtm,term=freq.terms,corThreshold=0.25,weighting=T)
这就是我所在的位置。主要思想是制作一个边属性列表,我们可以将其传递给 plot
.
library(tm)
library(graph)
library(igraph)
# Install Rgraphviz
source("http://bioconductor.org/biocLite.R")
biocLite("Rgraphviz")
data("acq")
dtm <- DocumentTermMatrix(acq,
control = list(weighting = function(x) weightTfIdf(x, normalize=FALSE),
stopwords = TRUE))
freq.terms <- findFreqTerms(dtm, lowfreq=10)[1:25]
assocs <- findAssocs(dtm, term=freq.terms, corlimit=0.25)
# Recreate edges, using code from plot.DocumentTermMatrix
m <- dtm
corThreshold <- 0.25
m <- as.matrix(m[, freq.terms])
c <- cor(m)
c[c < corThreshold] <- 0
c[is.na(c)] <- 0
diag(c) <- 0
ig <- graph.adjacency(c, mode="undirected", weighted=TRUE)
g1 <- as_graphnel(ig)
# Make edge labels
ew <- as.character(unlist(edgeWeights(g1)))
ew <- ew[setdiff(seq(along=ew), Rgraphviz::removedEdges(g1))]
names(ew) <- edgeNames(g1)
eAttrs <- list()
elabs <- paste(" ", round(as.numeric(ew), 2)) # so it doesn't print on top of the edge
names(elabs) <- names(ew)
eAttrs$label <- elabs
fontsizes <- rep(7, length(elabs))
names(fontsizes) <- names(ew)
eAttrs$fontsize <- fontsizes
plot(dtm, term=freq.terms, corThreshold=0.25, weighting=T,
edgeAttrs=eAttrs)
剩下的主要问题是绘图打印了两次边缘标签:一次使用默认设置,显然,另一次使用我们在 eAttrs
中指定的字体大小。
Edit. 看来为了让标签正确渲染,我们不能直接使用plot
。使用 renderGraph
(plot
调用)似乎有效。我们为边缘权重创建一个数值向量,并将其作为 lwd
参数传递给 renderEdgeInfo
。您必须更改标签的手动偏移量 (elabs <- paste(" ",...)
),以便标签与边缘的距离正确。
weights <- as.numeric(ew)
names(weights) <- names(ew)
edgeRenderInfo(g1) <- list(label=elabs, fontsize=fontsizes, lwd=weights*5)
nodeRenderInfo(g1) <- list(shape="box", fontsize=20)
g1 <- layoutGraph(g1)
renderGraph(g1)