Skip to content

Commit

Permalink
Explore geom functionality (#39)
Browse files Browse the repository at this point in the history
* add file

* first setup minimal convplot

* represent turns only and use talkr-specific names for data eg begin instead of xmin

* remove participant from required aesthetics

* fix issue from existing column nwords

* add geom_token

* fix namespace and ggproto docs

* rename plot function and use geoms

* add rank and frequency to token db

* add geom tests

* update docs for sourcecount

* remove unused file

* Update geom documentation with inherited parameters

* Add additional options to default turn plot

* comment out unused test

* update plot customization options

* Add geoms and default plot to workflow

* ggplot should be loaded also for aes use
  • Loading branch information
bvreede authored Jan 10, 2024
1 parent 20b6931 commit 36e61cf
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 47 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ export(add_lines)
export(calculate_timing)
export(check_quality)
export(convplot)
export(geom_token)
export(geom_turn)
export(GeomToken)
export(GeomTurn)
export(init)
export(inspect_language)
export(plot_quality)
export(plot_turns_tokens)
export(report_stats)
export(theme_turnPlot)
export(tokenize)
Expand Down
50 changes: 50 additions & 0 deletions R/geom_token.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#' Plot individual tokens
#'
#' From a separate data frame containing tokenized data, plot individual tokens
#' at their estimated time. Data must be provided separately, and should
#' contain a column with the participant (y) and a column with the time (x).
#'
#' @param data A tokenized data frame (see `tokenize()`).
#' @inheritParams ggplot2::layer
#' @inheritParams ggplot2::geom_point
#' @export
geom_token <- function(data, mapping = NULL,
stat = "identity", position = "identity",
..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
geom = GeomToken,
stat = stat,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(na.rm = na.rm,
...)
)
}

#' GeomToken
#'
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
GeomToken <- ggproto(
"GeomToken", Geom,
required_aes = c("x", "y"),

default_aes = aes(
fill = "grey90",
colour = "grey40",
alpha = 1,
size = 1,
shape = 19,
stroke = 1
),

draw_panel = function(data, panel_params, coord, ...) {
ggplot2::GeomPoint$draw_panel(data, panel_params, coord)
}
)

54 changes: 30 additions & 24 deletions R/geom_turn.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#' Show turn-taking in visualized conversations
#'
#' @param mapping Set of aesthetic mappings created by aes(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.
#' @param data The data to be displayed in this layer. There are three options: If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().
#' @param stat The statistical transformation to use on the data for this layer, either as a ggproto Geom subclass or as a string naming the stat stripped of the stat_ prefix (e.g. "count" rather than "stat_count")
#' @param position Position adjustment, either as a string naming the adjustment (e.g. "jitter" to use position_jitter), or the result of a call to a position adjustment function. Use the latter if you need to change the settings of the adjustment.
#' @param na.rm If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.
#' @param show.legend logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.
#' @param inherit.aes If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().
#' @param ... Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.
#' @param mapping Set of aesthetic mappings created by `ggplot2::aes()`.
#' Requires specification of `begin` and `end` of turns. Inherits from the default mapping at the
#' top level of the plot, if `inherit.aes` is set to `TRUE` (the default).
#' @inheritParams ggplot2::layer
#' @inheritParams ggplot2::geom_rect
#' @param height The height of the turn-taking rectangles
#' @export
#' @rdname geom_turn
geom_turn <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) {
..., na.rm = FALSE, height = 0.8, show.legend = NA, inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
Expand All @@ -22,6 +19,7 @@ geom_turn <- function(mapping = NULL, data = NULL,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(na.rm = na.rm,
height = height,
...)
)
}
Expand All @@ -34,26 +32,34 @@ geom_turn <- function(mapping = NULL, data = NULL,
#' @export
GeomTurn <- ggproto(
"GeomTurn", Geom,
required_aes = c("xmin", "xmax", "ymin", "ymax", "xpoint", "ypoint", "fillpoint"),
required_aes = c("begin", "end"),

default_aes = aes(
fill = "lightgrey",
fill = "grey80",
linewidth = 0,
alpha = 1,
colour = "white", # geom_point
size = 2,
shape = 21,
stroke = 1
alpha = 1
),

draw_panel = function(data, panel_params, coord, ...) {
rect_data <- transform(data, xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)
point_data <- transform(data, x = xpoint, y = -ypoint, fill = fillpoint)
extra_params = c("na.rm", "height"),

setup_data = function(data, params) {

data$height <- params$height

grid::grobTree(
ggplot2::GeomRect$draw_panel(rect_data, panel_params, coord),
ggplot2::GeomPoint$draw_panel(point_data, panel_params, coord)
)
data <- transform(data,
ymin = y - 0.5*height,
ymax = y + 0.5*height)

data
},

draw_panel = function(data, panel_params, coord, ...) {
rect_data <- transform(data,
xmin = begin,
xmax = end,
ymin = ymin,
ymax = ymax)
ggplot2::GeomRect$draw_panel(rect_data, panel_params, coord)
}
)

56 changes: 56 additions & 0 deletions R/plot_turns_tokens.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Plot a stretch of conversation
#'
#' Utterances are plotted as grey boxes, with frequently occurring tokens overlaid as colored dots.
#'
#' @param data a talkr dataset
#' @param begin start time of the plot in seconds (defaults to 0)
#' @param duration duration of the plot in seconds (defaults to 60)
#' @param maxrank maximum rank of tokens to plot (defaults to 10)
#' @param source name or number of the source to plot (defaults to 1, plotting the first source in the data)
#'
#' @return plot object
#' @export
plot_turns_tokens <- function(data,
begin = 0,
duration = 60,
maxrank = 10,
source = 1){
check_talkr(data)

if(is.numeric(source)){
sourcenr <- min(source, length(unique(data$source)))
source <- unique(data$source)[sourcenr]
}
data <- data[data$source == source,]

tokens <- data |>
tokenize()
tokens <- tokens[tokens$rank < maxrank,]

time_start <- begin * 1000
time_end <- time_start + duration * 1000
data <- data[data$begin > time_start & data$end < time_end,]

uids_included <- unique(data$uid)
tokens <- tokens[tokens$uid %in% uids_included,]

p <- data |>
ggplot2::ggplot(aes(
x = .data$end,
y = .data$participant)) +
talkr::geom_turn(aes(
begin = .data$begin,
end = .data$end)) +
talkr::geom_token(data = tokens,
aes(x = .data$relative_time,
y = .data$participant,
color = .data$rank)) +
talkr::theme_turnPlot() +
ggplot2::xlab("time (ms)") +
ggplot2::ggtitle(source)


return(p)
}


13 changes: 12 additions & 1 deletion R/tokenize.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ tokenize <- function(data, utterancecol = "utterance") {
dplyr::group_by(.data$uid) |>
dplyr::summarise(nwords = dplyr::n())

nwordstotal <- sum(count$nwords)

rank <- data |>
dplyr::group_by(.data$token) |>
dplyr::summarise(frequency = dplyr::n()/nwordstotal) |>
dplyr::arrange(dplyr::desc(.data$frequency)) |>
dplyr::mutate(rank = dplyr::row_number())

# merge timing data with token data and calculate timing
data <- data |>
dplyr::left_join(count, by = "uid") |>
dplyr::left_join(count, by = "uid", suffix = c("_orig","")) |>
dplyr::mutate(time_per_token = (.data$end - .data$begin) / .data$nwords,
starttime = .data$begin + (0.5 * .data$time_per_token),
relative_time = round(.data$starttime + (.data$tokenorder - 1) * .data$time_per_token, 0),
Expand All @@ -50,5 +58,8 @@ tokenize <- function(data, utterancecol = "utterance") {
TRUE ~ "middle")) |>
dplyr::select(.data$source, .data$uid, .data$participant, .data$nwords, .data$token, .data$order, .data$relative_time)

data <- data |>
dplyr::left_join(rank, by = "token")

return(data)
}
59 changes: 59 additions & 0 deletions man/geom_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 43 additions & 8 deletions man/geom_turn.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/ggplot2-ggproto.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 36e61cf

Please sign in to comment.