R 中的散点图或色散图
Scatter plot or dispersion plot in R
好的,所以我想要一个单一的情节,其中我有 "x" 部小说,我们将能够看到特定单词在所有小说中的分散情况。每本小说都有不同的长度(总字数),因此 "x" 轴必须是小说,而 "y" 轴必须是每本小说的长度。现在,我可以为每部小说创建一个单独的情节,但我想将它们全部放在一起。这是我目前所拥有的:
input.dir<-("corpus2")
files.v<-dir(input.dir, "\.txt$")
corpus<-corpus(files.v, input.dir)
tiempo<-tiempo(corpus)
noche<-palabra("día", corpus, tiempo)
dispersion(noche)
#corpus
corpus<-function(files.v, input.dir){
text.word.vector.l<-list()
for(i in 1:length(files.v)){
text.v <- scan(paste(input.dir, files.v[i], sep="/"), what="character", sep="\n")
Encoding(text.v)<-"UTF-8"
text.v <- paste(text.v, collapse=" ")
text.lower.v <- tolower(text.v)
text.words.v <- strsplit(text.lower.v, "\W")
text.words.v <- unlist(text.words.v)
text.words.v <- text.words.v[which(text.words.v!="")]
text.word.vector.l[[files.v[i]]] <- text.words.v
}
return(text.word.vector.l)
}
#tiempo
tiempo <- function(argument1){
tiempo.l<-list()
for (i in 1:length(argument1)){
time<-seq(1:length(argument1[[i]]))
tiempo.l[[files.v[i]]]<-time
}
return(tiempo.l)
}
#palabra
palabra<-function(keyword, argument1, argument2){
hits.l<-list()
for (i in 1:length(argument1)) {
hits.v<-which(argument1[[i]]==keyword)
hits.keyword.v<-rep(NA, length(argument2[[i]]))
hits.keyword.v[hits.v]<-1
hits.l[[files.v[i]]]<-hits.keyword.v
}
return(hits.l)
}
#dispersion
dispersion<-function(argument1){
options(scipen=5)
for (i in 1:length(argument1)) {
plot(argument1[[i]], main="Dispersion plot",
xlab="time", ylab="keyword", type="h", ylim=c(0,1), yaxt='n')
}
}
我怎样才能把它放在一起?这是我认为应该看起来像的图片:
我想做的是或多或少地把所有这些情节放在一起:
您的示例不可重现,因此下面的代码使用 Jane Austen 的小说使用 ggplot2 绘制单词位置。希望您可以根据您的需要调整此代码
library(tidyverse)
library(janeaustenr)
library(scales)
# Function to plot dispersion of a given vector of words in novels by Jane Austen
plot.dispersion = function(words) {
pattern = paste(words, collapse="|")
# Get locations of each input word in each text
# Adapted from Text Mining with R (https://www.tidytextmining.com/tfidf.html)
texts = austen_books() %>%
group_by(book) %>%
mutate(text = str_split(tolower(text), "\W")) %>%
unnest %>%
filter(text != "") %>%
mutate(word.num = 1:n(),
pct = word.num/n()) %>%
filter(grepl(pattern, text)) %>%
mutate(text = str_extract(text, pattern))
# Plot the word locations
ggplot(texts, aes(y=book, x=pct)) +
geom_point(shape="|", size=5) +
facet_grid(text ~ .) +
scale_x_continuous(labels=percent) +
labs(x="Percent of book", y="") +
theme_bw() +
theme(panel.grid.major.x=element_blank(),
panel.grid.minor.x=element_blank())
}
plot.dispersion(c("independent", "property"))
好的,所以我想要一个单一的情节,其中我有 "x" 部小说,我们将能够看到特定单词在所有小说中的分散情况。每本小说都有不同的长度(总字数),因此 "x" 轴必须是小说,而 "y" 轴必须是每本小说的长度。现在,我可以为每部小说创建一个单独的情节,但我想将它们全部放在一起。这是我目前所拥有的:
input.dir<-("corpus2")
files.v<-dir(input.dir, "\.txt$")
corpus<-corpus(files.v, input.dir)
tiempo<-tiempo(corpus)
noche<-palabra("día", corpus, tiempo)
dispersion(noche)
#corpus
corpus<-function(files.v, input.dir){
text.word.vector.l<-list()
for(i in 1:length(files.v)){
text.v <- scan(paste(input.dir, files.v[i], sep="/"), what="character", sep="\n")
Encoding(text.v)<-"UTF-8"
text.v <- paste(text.v, collapse=" ")
text.lower.v <- tolower(text.v)
text.words.v <- strsplit(text.lower.v, "\W")
text.words.v <- unlist(text.words.v)
text.words.v <- text.words.v[which(text.words.v!="")]
text.word.vector.l[[files.v[i]]] <- text.words.v
}
return(text.word.vector.l)
}
#tiempo
tiempo <- function(argument1){
tiempo.l<-list()
for (i in 1:length(argument1)){
time<-seq(1:length(argument1[[i]]))
tiempo.l[[files.v[i]]]<-time
}
return(tiempo.l)
}
#palabra
palabra<-function(keyword, argument1, argument2){
hits.l<-list()
for (i in 1:length(argument1)) {
hits.v<-which(argument1[[i]]==keyword)
hits.keyword.v<-rep(NA, length(argument2[[i]]))
hits.keyword.v[hits.v]<-1
hits.l[[files.v[i]]]<-hits.keyword.v
}
return(hits.l)
}
#dispersion
dispersion<-function(argument1){
options(scipen=5)
for (i in 1:length(argument1)) {
plot(argument1[[i]], main="Dispersion plot",
xlab="time", ylab="keyword", type="h", ylim=c(0,1), yaxt='n')
}
}
我怎样才能把它放在一起?这是我认为应该看起来像的图片:
我想做的是或多或少地把所有这些情节放在一起:
您的示例不可重现,因此下面的代码使用 Jane Austen 的小说使用 ggplot2 绘制单词位置。希望您可以根据您的需要调整此代码
library(tidyverse)
library(janeaustenr)
library(scales)
# Function to plot dispersion of a given vector of words in novels by Jane Austen
plot.dispersion = function(words) {
pattern = paste(words, collapse="|")
# Get locations of each input word in each text
# Adapted from Text Mining with R (https://www.tidytextmining.com/tfidf.html)
texts = austen_books() %>%
group_by(book) %>%
mutate(text = str_split(tolower(text), "\W")) %>%
unnest %>%
filter(text != "") %>%
mutate(word.num = 1:n(),
pct = word.num/n()) %>%
filter(grepl(pattern, text)) %>%
mutate(text = str_extract(text, pattern))
# Plot the word locations
ggplot(texts, aes(y=book, x=pct)) +
geom_point(shape="|", size=5) +
facet_grid(text ~ .) +
scale_x_continuous(labels=percent) +
labs(x="Percent of book", y="") +
theme_bw() +
theme(panel.grid.major.x=element_blank(),
panel.grid.minor.x=element_blank())
}
plot.dispersion(c("independent", "property"))