39
39
# ' data(presidential_debates_2012)
40
40
# ' (form1 <- with(presidential_debates_2012, formality(dialogue, person)))
41
41
# ' with(presidential_debates_2012, formality(form1, list(person, time))) #recycle form 1 for speed
42
+ # '
43
+ # ' plot(form1)
44
+ # ' plot(with(presidential_debates_2012, formality(form1, list(person, time))))
42
45
formality <- function (text.var , grouping.var = NULL , order.by.formality = TRUE , ... ){
43
46
44
47
UseMethod(" formality" )
@@ -83,8 +86,8 @@ formality.default <- function(text.var, grouping.var = NULL, order.by.formality
83
86
}
84
87
}
85
88
86
- formal <- c(' noun' , ' adjective ' , ' preposition ' , ' article' )
87
- contextual <- c(' pronoun ' , ' verb ' , ' adverb' , ' interjection' )
89
+ formal <- c(' noun' , ' preposition ' , ' adjective ' , ' article' )
90
+ contextual <- c(' verb ' , ' pronoun ' , ' adverb' , ' interjection' )
88
91
89
92
# # in other version this will be extracted
90
93
# =============================================
@@ -166,8 +169,8 @@ formality.Formality <- function(text.var, grouping.var = NULL, order.by.formalit
166
169
}
167
170
}
168
171
169
- formal <- c(' noun' , ' adjective ' , ' preposition ' , ' article' )
170
- contextual <- c(' pronoun ' , ' verb ' , ' adverb' , ' interjection' )
172
+ formal <- c(' noun' , ' preposition ' , ' adjective ' , ' article' )
173
+ contextual <- c(' verb ' , ' pronoun ' , ' adverb' , ' interjection' )
171
174
172
175
counts <- attributes(text.var )[[" counts" ]][[" counts" ]]
173
176
@@ -191,3 +194,130 @@ formality.Formality <- function(text.var, grouping.var = NULL, order.by.formalit
191
194
out
192
195
193
196
}
197
+
198
+
199
+
200
+ # ' Plots a Formality Object
201
+ # '
202
+ # ' Plots a Formality object.
203
+ # '
204
+ # ' @param x The Formality object
205
+ # ' @param plot logical. If \code{TRUE} the output is plotted.
206
+ # ' @param \ldots ignored.
207
+ # ' @return Returns a list of the three \pkg{ggplot2} objects that make the
208
+ # ' combined plot.
209
+ # ' @importFrom data.table :=
210
+ # ' @method plot Formality
211
+ # ' @export
212
+ plot.Formality <- function (x , plot = TRUE , ... ){
213
+
214
+ group.vars <- n <- warn <- contextual <- formal <- type <- NULL
215
+
216
+ grps <- attr(x , " group.var" )
217
+ pos <- attr(x , " pos.vars" )
218
+
219
+ # # Prepare the pos data
220
+ express1 <- paste0(" lapply(list(" , paste(pos , collapse = " ," ), " ), function(y) as.numeric(y/n))" )
221
+ express2 <- paste0(" paste(" , paste(grps , collapse = " , " ), " , sep = \" _\" )" )
222
+ pos_dat <- x [, c(grps , pos , " n" ), with = FALSE ][,
223
+ (pos ) : = eval(parse(text = express1 ))][,
224
+ ' group.vars' : = eval(parse(text = express2 ))][,
225
+ ' group.vars' : = factor (group.vars , levels = rev(group.vars ))][,
226
+ c(pos , " n" , " group.vars" ), with = FALSE ]
227
+
228
+ pos_dat_long <- data.table :: melt(pos_dat , id = c(" group.vars" , " n" ),
229
+ variable.name = " pos" , value.name = " proportion" )[,
230
+ pos : = factor (pos , levels = attr(x , " pos.vars" ))]
231
+
232
+ # # prepare the formality data
233
+ form_dat <- x [, c(grps , " n" , " F" ), with = FALSE ][,
234
+ ' group.vars' : = eval(parse(text = express2 ))][,
235
+ ' group.vars' : = factor (group.vars , levels = rev(group.vars ))][,
236
+ c(" group.vars" , " n" , " F" ), with = FALSE ][,
237
+ warn : = ifelse(n > 300 , FALSE , TRUE )]
238
+
239
+ # # prepare the contectual/formal data
240
+ con_form_dat <- x [, c(grps , " contextual" , " formal" , " n" ), with = FALSE ][,
241
+ (c(" contextual" , " formal" )) : = list (contextual / n , formal / n )][,
242
+ ' group.vars' : = eval(parse(text = express2 ))][,
243
+ ' group.vars' : = factor (group.vars , levels = rev(group.vars ))][,
244
+ c(" contextual" , " formal" , " n" , " group.vars" ), with = FALSE ]
245
+
246
+ con_form_long <- data.table :: melt(con_form_dat , id = c(" group.vars" , " n" ),
247
+ variable.name = " type" , value.name = " proportion" )[,
248
+ type : = factor (type , levels = c(" formal" , " contextual" ))]
249
+
250
+ con_form_plot <- ggplot2 :: ggplot(con_form_long ,
251
+ ggplot2 :: aes_string(x = " group.vars" , weight = " proportion" , fill = " type" )) +
252
+ ggplot2 :: geom_bar() +
253
+ ggplot2 :: coord_flip() +
254
+ ggplot2 :: xlab(NULL ) +
255
+ ggplot2 :: ylab(" " ) +
256
+ ggplot2 :: theme_bw() +
257
+ ggplot2 :: theme(
258
+ panel.grid = ggplot2 :: element_blank(),
259
+ # legend.position="bottom",
260
+ legend.title = ggplot2 :: element_blank(),
261
+ panel.border = ggplot2 :: element_blank(),
262
+ axis.line = ggplot2 :: element_line(color = " grey70" )
263
+ ) +
264
+ ggplot2 :: scale_y_continuous(labels = function (x ) paste0(round(x * 100 , 0 ), " %" ),
265
+ expand = c(0 ,0 )) +
266
+ ggplot2 :: scale_fill_manual(values = pals [c(2 , 6 ), 2 ])
267
+
268
+ form_plot <- ggplot2 :: ggplot(form_dat ,
269
+ ggplot2 :: aes_string(y = " group.vars" , x = " F" )) +
270
+ ggplot2 :: geom_point(ggplot2 :: aes_string(size = " n" ), alpha = .22 ) +
271
+ ggplot2 :: scale_size(range = c(1 , 7 ), name = " Text\n Length" ) +
272
+ ggplot2 :: geom_point(ggplot2 :: aes_string(color = " warn" ), size = 1.5 ) +
273
+ ggplot2 :: scale_color_manual(values = c(" black" , " red" ), guide = FALSE ) +
274
+ ggplot2 :: ylab(NULL ) +
275
+ ggplot2 :: xlab(" F Measure" ) +
276
+ ggplot2 :: theme_bw() +
277
+ ggplot2 :: theme(
278
+ # legend.position="bottom",
279
+ axis.title.x = ggplot2 :: element_text(size = 11 ),
280
+ # legend.title = ggplot2::element_blank(),
281
+ panel.border = ggplot2 :: element_blank(),
282
+ axis.line = ggplot2 :: element_line(color = " grey70" )
283
+ )
284
+
285
+ pos_heat_plot <- ggplot2 :: ggplot(pos_dat_long ,
286
+ ggplot2 :: aes_string(y = " group.vars" , x = " pos" , fill = " proportion" )) +
287
+ ggplot2 :: geom_tile() +
288
+ ggplot2 :: scale_fill_gradient(
289
+ labels = function (x ) paste0(round(x * 100 , 0 ), " %" ),
290
+ high = " #BF812D" ,
291
+ low = " white" ,
292
+ name = ggplot2 :: element_blank()
293
+ )+
294
+ ggplot2 :: ylab(NULL ) +
295
+ ggplot2 :: xlab(" Part of Speech" ) +
296
+ ggplot2 :: theme_bw() +
297
+ ggplot2 :: theme(
298
+ panel.grid = ggplot2 :: element_blank(),
299
+ # legend.position="bottom",
300
+ axis.title.x = ggplot2 :: element_text(size = 11 ),
301
+ legend.title = ggplot2 :: element_blank(),
302
+ panel.border = ggplot2 :: element_rect(color = " grey88" )
303
+ ) +
304
+ ggplot2 :: guides(fill = ggplot2 :: guide_colorbar(barwidth = .5 , barheight = 10 )) # +
305
+ # ggplot2::guides(fill = ggplot2::guide_colorbar(barwidth = 14, barheight = .5))
306
+
307
+ plotout1 <- gridExtra :: arrangeGrob(con_form_plot , form_plot ,
308
+ widths = grid :: unit(c(.5 , .5 ), " native" ), ncol = 2 )
309
+
310
+ plotout2 <- gridExtra :: arrangeGrob(plotout1 , pos_heat_plot , ncol = 1 )
311
+ if (isTRUE(plot )) gridExtra :: grid.arrange(plotout2 )
312
+ return (invisible (list (formality = form_plot , contextual_formal = con_form_plot , pos = pos_heat_plot )))
313
+ }
314
+
315
+
316
+ pals <- structure(list (pos = c(" noun" , " adjective" , " preposition" , " article" ,
317
+ " pronoun" , " verb" , " adverb" , " interjection" ), cols = c(" #8C510A" ,
318
+ " #BF812D" , " #DFC27D" , " #F6E8C3" , " #C7EAE5" , " #80CDC1" , " #35978F" ,
319
+ " #01665E" )), .Names = c(" pos" , " cols" ), row.names = c(NA , - 8L
320
+ ), class = " data.frame" )
321
+
322
+
323
+
0 commit comments