Skip to content

Instantly share code, notes, and snippets.

@trinker
Last active February 28, 2016 14:12
Show Gist options
  • Save trinker/813777dce48cf1f340f7 to your computer and use it in GitHub Desktop.
Save trinker/813777dce48cf1f340f7 to your computer and use it in GitHub Desktop.
Radar Plot
#' Business Radar Plot
#'
#' Given a \code{\link[base]{data.frame}} with \code{problems}, \code{category}, \code{importance} columns, create a radar plot of problems arranged in a military style radar sequence.
#'
#' @param data A \code{\link[base]{data.frame}} with the following columns:
#' \item{problems}{A character vector listing the problems}
#' \item{category}{A character vector grouping the problems}
#' \item{importance}{The time perceived importance of each item; an integer value between 1-100 with 100 being the most important and 1 being least}
#' \item{perceived}{An optional column of organization perceived importance used for point size; takes a value between 1-4;if \code{NULL} \code{importance} column will be used to generate this value}
#' @param legend.title A title for the legend.
#' @param seed A random seed to use for left to right \]code{jitter}ing.
#' @param jitter.factor A numeric value.
#' @param title A plot title.
#' @param size.range A numeric vector of length 2 denoting the min/max spoint sizes.
#' @return Returns a \code{ggplot2} radar plot.
#' @keywords radar
#' @export
#' @examples
#' \dontrun{
#' if (!require("pacman")) install.packages("pacman"); library(pacman)
#' p_load(dplyr, qdapDictionaries)
#'
#' set.seed(10)
#' data <- data_frame(
#' problems = sample(GradyAugmented, 30),
#' category = sample(sample(GradyAugmented, 5), 30, TRUE),
#' importance = sample(1:100, 30, TRUE)
#' )
#'
#' radar_plot(data)
#' }
radar_plot <- function(data, legend.title = "Perceptions\nof Criticality",
seed = NULL, jitter.factor = 2, title = NULL, size.range = c(2,8)){
if (is.null(data[['perceived']])){
data <- dplyr::mutate(data,
perceived = ceiling(3*(.001 + min_max(importance)))
)
}
set.seed(seed)
data <- dplyr::mutate(data,
category2 = factor(category),
category3 = jitter(as.numeric(category2), factor = jitter.factor),
importance = 100 * min_max(importance)
)
divides1 <- tail((1:length(unique(data[['category']]))) - .5, -1)
divides <- (1:length(unique(data[['category']]))) - .5
ggplot2::ggplot(data, ggplot2::aes_string(y = 'category3', x = 'importance')) +
ggplot2::geom_hline(yintercept = divides, color="grey70") +
ggplot2::geom_vline(xintercept = c(seq(0, 100, by = 20), 110),
color="grey70", linetype='dashed', alpha = .5) +
ggplot2::geom_vline(xintercept = c(0, 100), color="grey60") +
ggplot2::geom_point(shape=21, color="grey60", alpha=.8,
ggplot2::aes_string(size = 'perceived', fill='category')) +
ggplot2::scale_x_reverse() +
ggplot2::scale_y_continuous(breaks = seq_along(divides),
labels=levels(data[['category2']]), limits = c(min(divides), max(divides) + 1)) +
ggplot2::scale_size_continuous(range=size.range, name = legend.title) +
ggplot2::coord_polar(theta = 'y') +
ggplot2::theme_bw() +
ggplot2::guides(fill=FALSE, size = ggplot2::guide_legend(label.position = "top",
label.hjust = .5, override.aes = list(fill = "grey60", color = NA))) +
ggplot2::theme(
panel.grid = ggplot2::element_blank(),
axis.text.y = ggplot2::element_blank(),
axis.text.x = ggplot2::element_text(size=11),
axis.title = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
legend.position = 'bottom',
legend.key = ggplot2::element_blank(),
panel.border = ggplot2::element_blank()
) +
ggrepel::geom_text_repel(ggplot2::aes_string(label = 'problems'),
color='grey60', size=3) +
ggplot2::labs(title=title)
}
min_max <- function(x) {
(x - min(x))/(max(x) - min(x))
}
#=======================================
if (!require("pacman")) install.packages("pacman"); library(pacman)
p_load(dplyr, qdapDictionaries, ggrepel, ggplot2)
set.seed(10)
data <- data_frame(
problems = sample(GradyAugmented, 30),
category = sample(sample(GradyAugmented, 5), 30, TRUE),
importance = sample(1:100, 30, TRUE)
)
radar_plot(data)
#ggsave("radar_plot.png")
@trinker
Copy link
Author

trinker commented Feb 28, 2016

Imgur

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment