Skip to content

Instantly share code, notes, and snippets.

@internaut internaut/pcp.R
Created Sep 27, 2016

Embed
What would you like to do?
Comparison of Parallel Coordinate Plots for Discrete and Categorical Data in R
### generate questionnaire data
library(triangle)
set.seed(0)
q1_d1 <- round(rtriangle(1000, 1, 7, 5))
q1_d2 <- round(rtriangle(1000, 1, 7, 6))
q1_d3 <- round(rtriangle(1000, 1, 7, 2))
df <- data.frame(q1_d1 = factor(q1_d1), q1_d2 = factor(q1_d2), q1_d3 = factor(q1_d3))
### prepare data
library(dplyr)
# group by combinations and count
df_grouped <- df %>% group_by(q1_d1, q1_d2, q1_d3) %>% count()
# set an "id" string that denotes the value combination
df_grouped <- df_grouped %>% mutate(id = factor(paste(q1_d1, q1_d2, q1_d3, sep = '-')))
# sort by count and select top rows
df_grouped <- (df_grouped %>% arrange(desc(n)))[1:10,]
### ggparcoord from GGally
library(GGally)
ggparcoord(df, columns = 1:3, groupColumn = 'id', scale = 'globalminmax')
# # chaotic:
# df_ggpar <- df
# id_factors <- apply(df_ggpar[, c('q1_d1', 'q1_d2', 'q1_d3')], 1, as.factor)
# df_ggpar$id <- apply(id_factors, 2, paste, collapse = '-')
# ggparcoord(df_ggpar, columns = 1:3, groupColumn = 'id', scale = 'globalminmax')
### ggparallel
library(ggparallel)
ggparallel(list('q1_d1', 'q1_d2', 'q1_d3'), df, order = 0)
df_pcp <- as.data.frame(df_grouped)
ggparallel(list('q1_d1', 'q1_d2', 'q1_d3'), df_pcp, weight = 'n', order = 0)
### ggplot2
library(reshape2)
library(ggplot2)
# create tall skinny table
df_pcp <- melt(df_grouped, id.vars = c('id', 'n'))
df_pcp$value <- factor(df_pcp$value)
# draw
y_levels <- levels(factor(1:7))
#y_levels <- sort(levels(factor(1:7)), decreasing = T)
ggplot(df_pcp, aes(x = variable, y = value, group = id)) + # "group = id" is important!
geom_path(aes(size = n, color = id),
alpha = 0.5,
lineend = 'round', linejoin = 'round') +
scale_y_discrete(limits = y_levels, expand = c(0.5, 0)) +
scale_size(breaks = NULL, range = c(1, 7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.