word_associate: Find Associated Words

Description Usage Arguments Value See Also Examples

View source: R/word_associate.R

Description

Find words associated with a given word(s) or a phrase(s). Results can be output as a network graph and/or wordcloud.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
word_associate(text.var, grouping.var = NULL, match.string,
  text.unit = "sentence", extra.terms = NULL, target.exclude = NULL,
  stopwords = NULL, network.plot = FALSE, wordcloud = FALSE,
  cloud.colors = c("black", "gray55"), title.color = "blue",
  nw.label.cex = 0.8, title.padj = -4.5, nw.label.colors = NULL,
  nw.layout = NULL, nw.edge.color = "gray90",
  nw.label.proportional = TRUE, nw.title.padj = NULL,
  nw.title.location = NULL, title.font = NULL, title.cex = NULL,
  nw.edge.curved = TRUE, cloud.legend = NULL, cloud.legend.cex = 0.8,
  cloud.legend.location = c(-0.03, 1.03), nw.legend = NULL,
  nw.legend.cex = 0.8, nw.legend.location = c(-1.54, 1.41),
  legend.override = FALSE, char2space = "~~", ...)

Arguments

text.var

The text variable.

grouping.var

The grouping variables. Default NULL generates one word list for all text. Also takes a single grouping variable or a list of 1 or more grouping variables.

match.string

A list of vectors or vector of terms to associate in the text.

text.unit

The text unit (either "sentence" or "tot". This argument determines what unit to find the match string words within. For example if "sentence" is chosen the function pulls all text for sentences the match string terms are found in.

extra.terms

Other terms to color beyond the match string.

target.exclude

A vector of words to exclude from the match.string.

stopwords

Words to exclude from the analysis.

network.plot

logical. If TRUE plots a network plot of the words.

wordcloud

logical. If TRUE plots a wordcloud plot of the words.

cloud.colors

A vector of colors equal to the length of match.string +1.

title.color

A character vector of length one corresponding to the color of the title.

nw.label.cex

The magnification to be used for network plot labels relative to the current setting of cex. Default is .8.

title.padj

Adjustment for the title. For strings parallel to the axes, padj = 0 means right or top alignment, and padj = 1 means left or bottom alignment.

nw.label.colors

A vector of colors equal to the length of match.string +1.

nw.layout

layout types supported by igraph. See layout.

nw.edge.color

A character vector of length one corresponding to the color of the plot edges.

nw.label.proportional

logical. If TRUE scales the network plots across grouping.var to allow plot to plot comparisons.

nw.title.padj

Adjustment for the network plot title. For strings parallel to the axes, padj = 0 means right or top alignment, and padj = 1 means left or bottom alignment.

nw.title.location

On which side of the network plot (1=bottom, 2=left, 3=top, 4=right).

title.font

The font family of the cloud title.

title.cex

Character expansion factor for the title. NULL and NA are equivalent to 1.0.

nw.edge.curved

logical. If TRUE edges will be curved rather than straight paths.

cloud.legend

A character vector of names corresponding to the number of vectors in match.string. Both nw.legend and cloud.legend can be set separately; or one may be set and by default the other will assume those legend labels. If the user does not desire this behavior use the legend.override argument.

cloud.legend.cex

Character expansion factor for the wordcloud legend. NULL and NA are equivalent to 1.0.

cloud.legend.location

The x and y co-ordinates to be used to position the wordcloud legend. The location may also be specified by setting x to a single keyword from the list "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center". This places the legend on the inside of the plot frame at the given location.

nw.legend

A character vector of names corresponding to the number of vectors in match.string. Both nw.legend and cloud.legend can be set separately; or one may be set and by default the other will assume those legend labels. If the user does not desire this behavior use the legend.override argument.

nw.legend.cex

Character expansion factor for the network plot legend. NULL and NA are equivalent to 1.0.

nw.legend.location

The x and y co-ordinates to be used to position the network plot legend. The location may also be specified by setting x to a single keyword from the list "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center". This places the legend on the inside of the plot frame at the given location.

legend.override

By default if legend labels are supplied to either cloud.legend or nw.legend may be set and if the other remains NULL it will assume the supplied vector to the previous legend argument. If this behavior is not desired legend.override should be set to TRUE.

char2space

Currently a road to nowhere. Eventually this will allow the retention of characters as is allowed in trans_cloud already.

...

Other arguments supplied to trans_cloud.

Value

Returns a list:

word frequency matrices

Word frequency matrices for each grouping variable.

dialogue

A list of dataframes for each word list (each vector supplied to match.string) and a final dataframe of all combined text units that contain any match string.

match.terms

A list of vectors of word lists (each vector supplied to match.string).

Optionally, returns a word cloud and/or a network plot of the text unit containing the match.string terms.

See Also

trans_cloud, word_network_plot, wordcloud, graph.adjacency

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
## Not run: 
ms <- c(" I ", "you")
et <- c(" it", " tell", "tru")
out1 <- word_associate(DATA2$state, DATA2$person, match.string = ms, 
    wordcloud = TRUE,  proportional = TRUE, 
    network.plot = TRUE,  nw.label.proportional = TRUE, extra.terms = et,  
    cloud.legend =c("A", "B", "C"),
    title.color = "blue", cloud.colors = c("red", "purple", "gray70"))

#======================================
#Note: You don't have to name the vectors in the lists but I do for clarity
ms <- list(
    list1 = c(" I ", " you", "not"), 
    list2 = c(" wh")          
)

et <- list(
    B = c(" the", "do", "tru"), 
    C = c(" it", " already", "we")
)

out2 <- word_associate(DATA2$state, DATA2$person, match.string = ms, 
    wordcloud = TRUE,  proportional = TRUE, 
    network.plot = TRUE,  nw.label.proportional = TRUE, extra.terms = et,  
    cloud.legend =c("A", "B", "C", "D"),
    title.color = "blue", cloud.colors = c("red", "blue", "purple", "gray70"))

out3 <- word_associate(DATA2$state, list(DATA2$day, DATA2$person), match.string = ms)

#======================================
m <- list(
    A1 = c("you", "in"), #list 1
    A2 = c(" wh")        #list 2
)

n <- list(
    B = c(" the", " on"), 
    C = c(" it", " no")
)

out4 <- word_associate(DATA2$state, list(DATA2$day, DATA2$person), 
    match.string = m)
out5 <- word_associate(raj.act.1$dialogue, list(raj.act.1$person), 
    match.string = m)
out6 <- with(mraja1spl, word_associate(dialogue, list(fam.aff, sex), 
     match.string = m))
names(out6)
lapply(out6$dialogue, htruncdf, n = 20, w = 20)

#======================================
DATA2$state2 <- space_fill(DATA2$state, c("is fun", "too fun"))

ms <- list(
    list1 = c(" I ", " you", "is fun", "too fun"), 
    list2 = c(" wh")      
)

et <- list(
    B = c(" the", " on"), 
    C = c(" it", " no")
)

out7 <- word_associate(DATA2$state2, DATA2$person, match.string = ms, 
    wordcloud = TRUE,  proportional = TRUE, 
    network.plot = TRUE,  nw.label.proportional = TRUE, extra.terms = et,  
    cloud.legend =c("A", "B", "C", "D"),
    title.color = "blue", cloud.colors = c("red", "blue", "purple", "gray70"))
    
DATA2 <- qdap::DATA2

## End(Not run)

Example output

Loading required package: qdapDictionaries
Loading required package: qdapRegex
Loading required package: qdapTools
Loading required package: RColorBrewer
OpenJDK 64-Bit Server VM warning: Can't detect initial thread stack location - find_vma failed

Attaching package: 'qdap'

The following object is masked from 'package:base':

    Filter

[1] "cap.f"       "cap.m"       "escal.m"     "mont.f"      "mont.m"     
[6] "none.m"      "none.none"   "dialogue"    "match.terms"
$list1
   row  group unit                 text
1    3  cap.m    3 I mean, an we be in 
2    4  cap.m    4 Ay, while you live, 
3    5  cap.m    5 I strike quickly, be
4   13  cap.m   13 True; and therefore 
5   19  cap.m   19 Ay, the heads of the
6   20  cap.m   20 They must take it in
7   32  cap.m   32 Let us take the law 
8   36 mont.m   36 Do you bite your thu
9   38 mont.m   38 Do you bite your thu
10  41  cap.m   41 No, sir, I do not bi
11  42  cap.m   42 Do you quarrel, sir?
12  45  cap.m   45 If you do, sir, I am
13  46  cap.m   46 I serve as good a ma
14  50  cap.m   50 here comes one of my
15  52 mont.m   52             You lie.
16  53  cap.m   53 Draw, if you be men.
17  54  cap.m   54 Gregory, remember th
18  56 mont.m   56 Put up your swords; 
19  57  cap.m   57 What, art thou drawn
20  72  cap.f   72 why call you for a s

$list2
   row   group unit                 text
1    4   cap.m    4 Ay, while you live, 
2   17   cap.m   17 when I have fought w
3   19   cap.m   19 Ay, the heads of the
4   21   cap.m   21 Me they shall feel w
5   35   cap.m   35 I will bite my thumb
6   56  mont.m   56 Put up your swords; 
7   57   cap.m   57 What, art thou drawn
8   61   cap.m   61 What, drawn, and tal
9   69   cap.f   69  What noise is this?
10  72   cap.f   72 why call you for a s
11  78 escal.m   78            What, ho!
12  86  mont.m   86 Who set this ancient
13  87  mont.m   87 Speak, nephew, were 
14  90  mont.m   90 in the instant came 
15  91  mont.m   91 While we were interc
16  92  mont.f   92   O, where is Romeo?
17  95  mont.m   95 Madam, an hour befor
18  97  mont.m   97 I, measuring his aff
19 106  mont.m  106 Could we but learn f
20 108  mont.m  108 See, where he comes.

$any
   row  group unit                 text
1    3  cap.m    3 I mean, an we be in 
2    4  cap.m    4 Ay, while you live, 
3    5  cap.m    5 I strike quickly, be
4   13  cap.m   13 True; and therefore 
5   17  cap.m   17 when I have fought w
6   19  cap.m   19 Ay, the heads of the
7   20  cap.m   20 They must take it in
8   21  cap.m   21 Me they shall feel w
9   32  cap.m   32 Let us take the law 
10  35  cap.m   35 I will bite my thumb
11  36 mont.m   36 Do you bite your thu
12  38 mont.m   38 Do you bite your thu
13  41  cap.m   41 No, sir, I do not bi
14  42  cap.m   42 Do you quarrel, sir?
15  45  cap.m   45 If you do, sir, I am
16  46  cap.m   46 I serve as good a ma
17  50  cap.m   50 here comes one of my
18  52 mont.m   52             You lie.
19  53  cap.m   53 Draw, if you be men.
20  54  cap.m   54 Gregory, remember th

Warning message:
system call failed: Cannot allocate memory 

qdap documentation built on Nov. 20, 2017, 5:09 p.m.