Permalink
Browse files

add tot_plot

  • Loading branch information...
1 parent cad3ead commit ae6dde59deaebfa9eefe47f6d075f041eddeae72 @trinker committed Mar 18, 2013
Showing with 150 additions and 1 deletion.
  1. +1 −0 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +3 −0 NEWS
  4. +92 −0 R/tot_plot.R
  5. +1 −1 inst/maintenance.R
  6. +1 −0 inst/staticdocs/index.R
  7. +51 −0 man/tot_plot.Rd
View
@@ -191,3 +191,4 @@ Collate:
'word_stats.R'
'words.R'
'xnoy.R'
+ 'tot_plot.R'
View
@@ -125,6 +125,7 @@ export(termco.d)
export(termco2mat)
export(text2color)
export(TOT)
+export(tot_plot)
export(trans.cloud)
export(trans.venn)
export(Trim)
View
3 NEWS
@@ -9,6 +9,9 @@ BUG FIXES
NEW FEATURES
+* `tot_plot` a visualizing function that uses a bar graph to visualize patterns
+ in sentence length and grouping variables by turn of talk.
+
MINOR FEATURES
CHANGES
View
@@ -0,0 +1,92 @@
+#' Visualize Word Length by Turn of Talk
+#'
+#' Uses a bar graph to visualize patterns in sentence length and grouping
+#' variables by turn of talk.
+#'
+#' @param dataframe A dataframe that contains the text variable and optionally
+#' the grouping.var and tot variables.
+#' @param text.var The text variable (character string).
+#' @param grouping.var The grouping variables (character string).
+#' @param tot The turn of talk variable (character string). May be TRUE (assumes
+#' "tot" is the variable name), FALSE (use row numbers), or a character string
+#' of the turn of talk column.
+#' @param ylab Optional y label.
+#' @param xlab Optional x label.
+#' @param space The amount space between bars (ranging between 1 and 0).
+#' @return Invisibly returns the ggplot2 object.
+#' @keywords sentence, split, turn-of-talk
+#' @import ggplot2
+#' @export
+#' @examples
+#' \dontrun{
+#' dataframe <- sentSplit(DATA, "state")
+#' tot_plot(dataframe, "state")
+#' tot_plot(DATA, "state", tot=FALSE)
+#' tot_plot(dataframe, "state", space=.03)
+#' tot_plot(dataframe, "state", "sex")
+#' tot_plot(mraja1, "dialogue", "fam.aff", tot=FALSE)
+#' tot_plot(mraja1, "dialogue", c("sex", "fam.aff"), tot=FALSE)
+#' }
+tot_plot <- function(dataframe, text.var, grouping.var = NULL, tot = TRUE,
+ ylab=NULL, xlab=NULL, space=0) {
+ DF <- dataframe
+ if (isTRUE(tot)) {
+ if(!any(colnames(dataframe) %in% "tot")) {
+ warning("Turn of talk (\"tot\") column not found; using rows instead")
+ tot2 <- dataframe[, "tot"] <- 1:nrow(dataframe)
+ dataframe <- dataframe[, c("tot", text.var)]
+ } else {
+ tot2 <- tot <- TOT(dataframe[, "tot"])
+ dataframe <- sentCombine(dataframe[, text.var], tot)
+ tot <- TRUE
+ }
+ }
+ if (!tot) {
+ tot2 <- dataframe[, "tot"] <- 1:nrow(dataframe)
+ dataframe <- dataframe[, c("tot", text.var)]
+ }
+ if (is.character(tot)) {
+ if(!any(colnames(dataframe) %in% tot)) {
+ warning("Turn of talk (", tot, ") column not found; using rows instead")
+ tot2 <- dataframe[, "tot"] <- 1:nrow(dataframe)
+ dataframe <- dataframe[, c("tot", text.var)]
+ } else {
+ tot2 <- tot
+ dataframe <- sentCombine(dataframe[, text.var], tot)
+ }
+ }
+ if (!is.null(grouping.var)) {
+ G <- paste(grouping.var, collapse="&")
+ if (ncol(DF[, grouping.var, drop=FALSE]) > 1) {
+ dataframe[, "group"] <- sapply(split(paste2(DF[, grouping.var]), tot2), unique)
+ } else {
+ dataframe[, "group"] <- sapply(split(DF[, grouping.var], tot2), unique)
+ }
+ colnames(dataframe)[3] <- G
+ }
+ colnames(dataframe)[2] <- "text.var"
+ dataframe[, "word.count"] <- wc(dataframe[, "text.var"])
+ if (is.null(xlab)) {
+ Xlab <- "Turn of Talk"
+ }
+ if (is.null(ylab)) {
+ Ylab <- "Word Count"
+ }
+ dataframe[, "tot"] <- factor(dataframe[, "tot"],
+ levels=sort(as.numeric(as.character(dataframe[, "tot"]))))
+ dataframe["space"] <- rep(space, nrow(dataframe))
+ theplot <- ggplot(dataframe, aes(tot, word.count, width=1-space))
+ if (ncol(dataframe) == 5) {
+ GR <- colnames(dataframe)[3]
+ colnames(dataframe)[3] <- "group"
+ theplot <- theplot + geom_bar(stat="identity", aes(fill=group), data=dataframe) +
+ labs(fill = caps(gsub("&", " & ", GR, fixed=TRUE), all=TRUE))
+ } else {
+ theplot <- theplot + geom_bar(stat="identity")
+ }
+ theplot <- theplot + ylab(Ylab) + xlab(Xlab) +
+ scale_y_continuous(expand = c(0,0)) +
+ theme(axis.text.x=element_blank(), axis.ticks.x=element_blank())
+ print(theplot)
+ invisible(theplot)
+}
View
@@ -1,5 +1,5 @@
#========================
-#staticdocs build version
+#staticdocs dev version
#========================
#packages
library(highlight); library(qdap); library(staticdocs); library(acc.roxygen2)
View
@@ -169,6 +169,7 @@ list(
"plot.word_stats",
"qheat",
"rank_freq_mplot",
+ "tot_plot",
"trans.cloud",
"trans.venn",
"word.network.plot"
View
@@ -0,0 +1,51 @@
+\name{tot_plot}
+\alias{tot_plot}
+\title{Visualize Word Length by Turn of Talk}
+\usage{
+ tot_plot(dataframe, text.var, grouping.var = NULL,
+ tot = TRUE, ylab = NULL, xlab = NULL, space = 0)
+}
+\arguments{
+ \item{dataframe}{A dataframe that contains the text
+ variable and optionally the grouping.var and tot
+ variables.}
+
+ \item{text.var}{The text variable (character string).}
+
+ \item{grouping.var}{The grouping variables (character
+ string).}
+
+ \item{tot}{The turn of talk variable (character string).
+ May be TRUE (assumes "tot" is the variable name), FALSE
+ (use row numbers), or a character string of the turn of
+ talk column.}
+
+ \item{ylab}{Optional y label.}
+
+ \item{xlab}{Optional x label.}
+
+ \item{space}{The amount space between bars (ranging
+ between 1 and 0).}
+}
+\value{
+ Invisibly returns the ggplot2 object.
+}
+\description{
+ Uses a bar graph to visualize patterns in sentence length
+ and grouping variables by turn of talk.
+}
+\examples{
+\dontrun{
+dataframe <- sentSplit(DATA, "state")
+tot_plot(dataframe, "state")
+tot_plot(DATA, "state", tot=FALSE)
+tot_plot(dataframe, "state", space=.03)
+tot_plot(dataframe, "state", "sex")
+tot_plot(mraja1, "dialogue", "fam.aff", tot=FALSE)
+tot_plot(mraja1, "dialogue", c("sex", "fam.aff"), tot=FALSE)
+}
+}
+\keyword{sentence,}
+\keyword{split,}
+\keyword{turn-of-talk}
+

0 comments on commit ae6dde5

Please sign in to comment.