Skip to content

Commit

Permalink
Minor edits to the vignette, pander not required any longer.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattflor committed Aug 9, 2020
1 parent f9a3e90 commit a971202
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 80 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Expand Up @@ -20,7 +20,6 @@ Imports:
Suggests:
dplyr,
knitr,
pander,
shiny,
tibble
VignetteBuilder: knitr
Expand Down
78 changes: 14 additions & 64 deletions inst/examples/example.R
Expand Up @@ -14,7 +14,7 @@ m

# default call; group arc length is determined by the row sums; here, this is
# the number of people with different hair colors asked what hair color they
# prefer; this probably reflects how many people there were with the different
# prefer; this reflects how many people there were with the different
# hair colors
chorddiag(m, showGroupnames = T)

Expand All @@ -23,8 +23,8 @@ chorddiag(m, showGroupnames = T)
groupColors <- c("#000000", "#FFDD89", "#957244", "#F26223")
chorddiag(m, groupColors = groupColors, groupnamePadding = 50, margin = 100,
tooltipGroupConnector = " prefer ")
# by transposing the matrix, group arc length is determined by the hair color
# numbers prefered:
# by transposing the matrix, group arc length is determined by how ofthen the
# different hair colors were prefered:
chorddiag(t(m), groupColors = groupColors, groupnamePadding = 50, margin = 100,
tooltipGroupConnector = " prefered by ")
# we can also normalize preference by row sums thus expressing for each hair
Expand All @@ -35,72 +35,22 @@ chorddiag(100*m/rowSums(m), groupColors = groupColors, groupnamePadding = 30,
tooltipGroupConnector = " prefer ", tooltipUnit = " %",
precision = 2)


# world migrant stocks
library(migration.indices)
library(RColorBrewer)
data("migration.world") # this 226x226 matrix has migrant stock numbers from 1990
# where row name gives the country of origin and column
# name gives the destination country
sort.by.orig <- sort(rowSums(migration.world), decreasing = TRUE, index.return = TRUE)
mig.sorted.by.orig <- migration.world[sort.by.orig$ix, sort.by.orig$ix]
row.names(mig.sorted.by.orig) <- names(sort.by.orig$x)

n <- dim(mig.sorted.by.orig)[1]
groupColors <- rep(brewer.pal(12, "Set3")[c(1:8, 10:12)], length.out = n)
groupNames <- rep("", n)
ix <- c(1:50, seq(52, 100, by = 2),
seq(105, 150, by = 5),
160, 180, 226)
groupNames[ix] <- colnames(mig.sorted.by.orig)[ix]
tooltipNames <- colnames(mig.sorted.by.orig)
chorddiag(mig.sorted.by.orig, groupPadding = 0,
showTicks = FALSE, margin = 120,
chordedgeColor = NULL, groupColors = groupColors,
groupnameFontsize = 10,
groupNames = groupNames,
groupnamePadding = 10,
tooltipNames = tooltipNames,
fadeLevel = 0)

mig.transposed <- t(migration.world)
sort.by.dest <- sort(rowSums(mig.transposed), decreasing = TRUE, index.return = TRUE)
mig.sorted.by.dest <- mig.transposed[sort.by.dest$ix, sort.by.dest$ix]
row.names(mig.sorted.by.dest) <- names(sort.by.dest$x)

n <- dim(mig.sorted.by.dest)[1]
groupColors <- rep(brewer.pal(12, "Set3")[c(1:8, 10:12)], length.out = n)
groupNames <- rep("", n)
ix <- c(1:40, seq(42, 60, by = 2),
seq(65, 100, by = 5),
110, 130, 150, 226)
groupNames[ix] <- colnames(mig.sorted.by.dest)[ix]
tooltipNames <- colnames(mig.sorted.by.dest)
chorddiag(mig.sorted.by.dest, groupPadding = 0,
showTicks = FALSE, margin = 120,
chordedgeColor = NULL, groupColors = groupColors,
groupnameFontsize = 10,
groupNames = groupNames,
groupnamePadding = 10,
tooltipNames = tooltipNames,
tooltipGroupConnector = " &#x25c0; ",
fadeLevel = 0)


# Bipartite chord diagram with Titanic data
if (requireNamespace("dplyr", quietly = TRUE)) {
titanic_tbl <- dplyr::tbl_dt(Titanic)
library(dplyr)
titanic_tbl <- tibble::as_tibble(Titanic)
titanic_tbl <- titanic_tbl %>%
mutate_each(funs(factor), Class:Survived)
mutate(across(where(is.character), as.factor))
by_class_survival <- titanic_tbl %>%
group_by(Class, Survived) %>%
summarize(Count = sum(N))
m <- matrix(by_class_survival$Count, nrow = 4, ncol = 2)
dimnames(m) <- list(Class = levels(titanic_tbl$Class),
Survival = levels(titanic_tbl$Survived))
m
summarise(Count = sum(n)) %>%
ungroup()
titanic.mat <- matrix(by_class_survival$Count, nrow = 4, ncol = 2, byrow = TRUE)
dimnames(titanic.mat ) <- list(Class = levels(titanic_tbl$Class),
Survival = levels(titanic_tbl$Survived))
print(titanic.mat)
groupColors <- c("#2171b5", "#6baed6", "#bdd7e7", "#bababa", "#d7191c", "#1a9641")
chorddiag(m, type = "bipartite", groupColors = groupColors,
tooltipGroupConnector = " in ",
chorddiag(titanic.mat, type = "bipartite",
groupColors = groupColors,
tickInterval = 50)
}
4 changes: 4 additions & 0 deletions inst/examples/shiny/server.R
@@ -1,3 +1,7 @@
if (!requireNamespace("shiny", quietly = TRUE)) {
stop("Package \"shiny\" needed to run the example app. Please install it.",
call. = FALSE)
}
library(shiny)
library(chorddiag)

Expand Down
15 changes: 0 additions & 15 deletions vignettes/chorddiagram-vignette.Rmd
Expand Up @@ -20,10 +20,6 @@ if (!requireNamespace("dplyr", quietly = TRUE)) {
stop("Package \"dplyr\" needed to build the vignette. Please install it.",
call. = FALSE)
}
if (!requireNamespace("pander", quietly = TRUE)) {
stop("Package \"pander\" needed to build the vignette. Please install it.",
call. = FALSE)
}
```


Expand Down Expand Up @@ -78,11 +74,6 @@ dimnames(m) <- list(have = haircolors,
print(m)
```

```{r, echo = FALSE, results = 'asis'}
pander::pandoc.table(m, style = "rmarkdown",
caption = "Hair color preference data. Row names show what hair color people have, and column names show what hair color they prefer for a dating partner.")
```

Then, we can pass this matrix to the `chorddiag` function to create the chord diagram:

```{r, eval = FALSE, fig.width = 8}
Expand Down Expand Up @@ -136,12 +127,6 @@ print(titanic.mat)
Note that we labeled the dimensions of the matrix by assigning a named list to `dimnames`.
The dimension labels (here: "Class" and "Survival") will automatically be used in the chord diagram.

```{r, echo = FALSE, results = 'asis'}
pander::pandoc.table(titanic.mat,
type = "rmarkdown",
caption = "Titanic data. Counts of people who died ('No') or survived ('Yes') grouped by membership to passenger class and crew.")
```

We can create a "bipartite" chord diagram for this matrix by setting `type = "bipartite"`.

```{r, eval = FALSE}
Expand Down

0 comments on commit a971202

Please sign in to comment.