Skip to content

Commit f8cc6a8

Browse files
author
klaus
committed
Added ggassoc, export of jazzomat_palette, fixed bug in build_bigram_stack
1 parent 6b9ce12 commit f8cc6a8

10 files changed

+173
-12
lines changed

.Rbuildignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.travis.yml
44
^Makefile
55
data_raw
6+
^data/.+$

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: parkR
22
Type: Package
33
Title: Generating monophonic jazz solos for chord sequences
4-
Version: 0.5.0
4+
Version: 0.5.1
55
Authors@R: c(
66
person("Klaus", "Frieler", email = "[email protected]", role = c("aut","cre"))
77
)

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export(fuzzyint_hist)
2121
export(generate_solo)
2222
export(get_cdpcx)
2323
export(get_scale_degree_code)
24+
export(ggassoc)
2425
export(int_hist)
2526
export(int_span)
2627
export(key_analysis)

R/bigram_stack.R

+10-7
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ build_bigram_stack <- function(x, max_level = 3, sd_threshold = 1.0, ids = NULL,
6262
ids <- ids$id
6363
}
6464
messagef("Adding ids")
65-
if(!early_break){
66-
ret <- ret %>% arrange(pos)
67-
ret$id <- rep(ids, each = max_level)
68-
}
69-
else{
70-
ret <- ret %>% left_join(tibble(pos = 1:length(ids), id = unique(ids)), by = "pos")
71-
}
65+
max_level <- max(ret$level)
66+
ret <- ret %>% arrange(pos)
67+
ret$id <- rep(ids, each = max_level)
68+
# if(early_break){
69+
# ret <- ret %>% arrange(pos)
70+
# ret$id <- rep(ids, each = max_level)
71+
# }
72+
# else{
73+
# #ret <- ret %>% left_join(tibble(pos = 1:length(ids), id = unique(ids)), by = "pos")
74+
# }
7275
messagef("Adding document frequencies")
7376
ret <- ret %>% group_by(bigram_id) %>% mutate(DF = n_distinct(id)) %>% ungroup()
7477
} else{

R/data.R

+8
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,11 @@ NULL
140140
#' @name labels
141141
#' @docType data
142142
NULL
143+
144+
145+
#' jazzomat_palette
146+
#'
147+
#' Palette used in the "Inside the Jazzomat" book
148+
#' @name jazzomat_palette
149+
#' @docType data
150+
NULL

R/ggassoc.R

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
chisq.residuals <- function (tab, digits = 2, std = FALSE, raw = FALSE)
2+
{
3+
if (all(std, raw))
4+
stop("Choose between standardized and raw residuals.")
5+
k = stats::chisq.test(tab)
6+
if (raw) {
7+
res <- k$observed - k$expected
8+
}
9+
else if (std) {
10+
res <- k$stdres
11+
}
12+
else {
13+
res <- k$residuals
14+
}
15+
round(res, digits)
16+
}
17+
18+
#' ggassoc
19+
#' Plots chisquare residuals for 2 x N tables, similar to vcd::asso
20+
#' @param cont_tab (table) 2 x N Contingency table
21+
#' @param x_lab (string) Label for x-axis
22+
#' @param y_lab (string) Label for y-axis
23+
#' @param subtitle (string) subtitle for plot
24+
#' @param spread (numeric) Spread factor for boxes
25+
#' @param text_size (integer) Text size
26+
#' @param fill_colors (string) 2-element vector of color values for filling boxes, defaults to black and red
27+
#'
28+
#' @return ggplot2
29+
#' @export
30+
ggassoc <- function(cont_tab, x_lab = "", y_lab = "", subtitle = "", spread = 2.25, text_size = 3, fill_colors = c("black", "red")){
31+
names <- dimnames(cont_tab)
32+
if(length(names[[1]]) != 2){
33+
stop("Currently only available for 2 x N tables!")
34+
}
35+
if(is.null(x_lab) || nchar(x_lab) == 0){
36+
x_lab <- "target"
37+
}
38+
if(is.null(y_lab) || nchar(y_lab) == 0){
39+
y_lab <- "group"
40+
}
41+
#browser()
42+
cont_tab <- cont_tab[,colSums(cont_tab) != 0]
43+
group_labels <- names[[1]]
44+
target_labels <- names[[2]]
45+
num_cats <- length(target_labels)
46+
resid_df <- chisq.residuals(cont_tab) %>%
47+
t() %>%
48+
as.data.frame() %>%
49+
set_names(x_lab, y_lab, "residual") %>%
50+
mutate(resid_norm = residual/max(abs(residual), na.rm = T),
51+
group = factor(!!sym(y_lab)))
52+
sum_stats <- (colSums(cont_tab)/sum(cont_tab)) %>%
53+
as.data.frame() %>%
54+
rownames_to_column() %>%
55+
set_names("target_cat", "width") %>%
56+
mutate(width = sqrt(2 * width / max(width))) %>%
57+
mutate(xpos = cumsum(c(0, width[-length(width)])))
58+
max_resid <- max(abs(resid_df$resid_norm))
59+
#browser()
60+
resid_df <- resid_df %>%
61+
mutate(group_f = spread * (as.numeric(group) - 1) - spread/2,
62+
target_f = as.numeric(factor(!!sym(x_lab)))) %>%
63+
mutate(xmin = rep(sum_stats$xpos, 2),
64+
xmax = rep(sum_stats$xpos + sum_stats$width, 2),
65+
ymin = group_f,
66+
ymax = group_f + resid_norm,
67+
sign_dev = residual > 0)
68+
#browser()
69+
if(nchar(subtitle) == 0){
70+
subtitle <- sprintf("%s x %s", x_lab, y_lab)
71+
}
72+
q <- resid_df %>% ggplot(aes())
73+
q <- q + geom_rect(data = resid_df, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = sign_dev), color = "white")
74+
q <- q + theme_classic()
75+
q <- q + theme(legend.position = "none",
76+
axis.line.x = element_blank(),
77+
axis.line.y = element_blank(),
78+
axis.ticks.y = element_blank(),
79+
axis.text.y = element_text(size = 12, face = "bold"))
80+
q <- q + labs(x = "", y = "",
81+
subtitle = subtitle, title = "Chi-Square Residuals")
82+
q <- q + scale_fill_manual(values = fill_colors[1:2])
83+
q <- q + scale_x_continuous(breaks = NULL)
84+
q <- q + scale_y_continuous(breaks = c(-spread/2, spread/2), labels = group_labels)
85+
q <- q + geom_text(data = sum_stats,
86+
aes(x = xpos + .5 * width,
87+
y = .1 + .1 * (as.integer(factor(target_cat)) %% 3- 1),
88+
label = target_cat, hjust = 0.5, size = width ))
89+
q <- q + scale_size(range = c(3, 5))
90+
q
91+
}

R/plot_util.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ add_histogram <- function(q, percentage = T, binwidth = NULL, fill_var = NULL, c
328328
cdpcx_hist <- function(data, id = NULL, colour_chromatic = T, percentage = T, fill_var = NULL, cdpcx_col = "cdpcx_raw_all"){
329329
tmp <- select_by_id(data, id)
330330
tmp <- tmp[tmp[[cdpcx_col]] != "X",]
331+
tmp <- tmp[tmp[[cdpcx_col]] != "",]
331332
if (nrow(tmp) == 0){
332333
return(NULL)
333334
# tmp <- select_by_id(data, id)
@@ -442,14 +443,14 @@ mcm_hist <- function(data, id = NULL, percentage = T, fill_var = NULL, mcm48_col
442443
#'
443444
#' @return ggplot2 object
444445
#' @export
445-
int_hist <- function(data, id = NULL, cut_off = 25, percentage = T, int_col = "int_raw"){
446+
int_hist <- function(data, id = NULL, cut_off = 25, percentage = T, int_col = "int_raw", fill_var = NULL, reduced_labels = T){
446447
tmp <- select_by_id(data, id)
447448
tmp <- tmp[!is.na(tmp[[int_col]]),]
448449
tmp <- tmp[abs(tmp[[int_col]]) < cut_off,]
449450
ext <- max(abs(min(tmp[[int_col]])), abs(max(tmp[[int_col]])))
450451
tmp[[int_col]] <- factor(tmp[[int_col]], levels = -ext:ext)
451452
labels <- rep("", 49)
452-
if (ext > 14){
453+
if (reduced_labels || ext > 14){
453454
marks = c(-24, -19, -12, -7, -5, -2, 0, 2, 7, 5, 12, 19, 24)
454455
labels[marks + 25] = marks
455456
labels <- labels[(25 - ext):(25 + ext + 1)]
@@ -458,7 +459,7 @@ int_hist <- function(data, id = NULL, cut_off = 25, percentage = T, int_col = "i
458459
labels = -ext:ext
459460
}
460461
q <- ggplot(tmp, aes(x=!!sym(int_col)))
461-
q <- add_geom_bar(q, percentage)
462+
q <- add_geom_bar(q, percentage, fill_var = fill_var)
462463

463464
q <- q + get_default_theme() + theme(legend.position = "none")
464465
q <- q + scale_x_discrete(name = "Semitone Interval", drop = FALSE, labels = labels)

man/ggassoc.Rd

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/int_hist.Rd

+9-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/jazzomat_palette.Rd

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)