I found code online for a custom function for a spiderplot (thank you Zvonimir Boban: https://towardsdatascience.com/how-to-make-a-spider-chart-in-r-using-ggplot2-85a4f1898cab). The chart was successful, I can't figure out how to add a background image without having it cover my plot.
I am also struggling to customize the chart (e.g. remove the background panel, change positioning of axis labels) and am unable to do so with the normal functions I know how to do in ggplot. Are these issues happening because because the ggspider function I used is not actually in the ggplot function?
This is the code for the ggspider function:
library(dplyr)
library(tidyr)
library(tibble)
library(purrr)
library(scales)
library(ggplot2)
ggspider <- function(p_data,
legend_title = "Group",
background_color = "gray97",
area_fill = TRUE,
central_distance = 0.2,
axis_name_offset = 0.2){
circle_coords <- function(r, n_axis = ncol(p_data) - 1){
fi <- seq(0, 2*pi, (1/n_axis)*2*pi) + pi/2
x <- r*cos(fi)
y <- r*sin(fi)
tibble(x, y, r)
}
(step_1 \<- map_df(seq(0, 1, 0.25) + central_distance, circle_coords) %\>%
ggplot(aes(x, y)) +
geom_polygon(data = circle_coords(1 + central_distance), alpha = 1, fill = background_color) +
geom_path(aes(group = r), lty = 2, alpha = 0.5) +
theme_void())
axis_coords \<- function(n_axis){
fi \<- seq(0, (1 - 1/n_axis)*2*pi, (1/n_axis)*2*pi) + pi/2
x1 \<- central_distance*cos(fi)
y1 \<- central_distance*sin(fi)
x2 \<- (1 + central_distance)\*cos(fi)
y2 \<- (1 + central_distance)\*sin(fi)
tibble(x = c(x1, x2), y = c(y1, y2), id = rep(1:n_axis, 2))
}
text_data \<- p_data %\>%
select(-group) %\>%
map_df(\~ min(.) + (max(.) - min(.)) \* seq(0, 1, 0.25)) %\>%
mutate(r = seq(0, 1, 0.25)) %\>%
pivot_longer(-r, names_to = "parameter", values_to = "value")
text_coords \<- function(r, n_axis = ncol(p_data) - 1){
fi \<- seq(0, (1 - 1/n_axis)*2*pi, (1/n_axis)*2*pi) + pi/2 + 0.01*2*pi/r
x \<- r*cos(fi)
y \<- r*sin(fi)
tibble(x, y, r = r - central_distance)}
labels_data \<- map_df(seq(0, 1, 0.25) + central_distance, text_coords) %\>%
bind_cols(text_data %\>% select(-r))
rescaled_coords \<- function(r, n_axis){
fi \<- seq(0, 2*pi, (1/n_axis)2*pi) + pi/2
tibble(r, fi) %\>% mutate(x = rcos(fi), y = r\*sin(fi)) %\>% select(-fi)
}
rescaled_data \<- p_data %\>%
mutate(across(-group, rescale)) %\>%
mutate(copy = pull(., 2)) %\>%
pivot_longer(-group, names_to = "parameter", values_to = "value") %\>%
group_by(group) %\>%
mutate(coords = rescaled_coords(value + central_distance, ncol(p_data) - 1)) %\>%
unnest
step_1 +
geom_line(data = axis_coords(ncol(p_data) - 1),
aes(x, y, group = id), alpha = 0.3) +
geom_point(data = rescaled_data,
aes(x, y, group = group, col = group), size = 3) +
geom_path(data = rescaled_data,
aes(x, y, group = group, col = group), size = 1) +
{if(area_fill == TRUE) geom_polygon(data = rescaled_data,
aes(x, y, group = group,
col = group, fill = group),
size = 1, alpha = 0.05, show.legend = FALSE)} +
geom_text(data = labels_data,
aes(x, y, label = value), alpha = 0.65) +
geom_text(data = text_coords(1 + central_distance + axis_name_offset),
aes(x, y), label = labels_data$parameter\[1:(ncol(p_data)-1)\]) +
labs(col = legend_title) +
theme(legend.position = "bottom",
legend.text = element_text(size = 12),
legend.title = element_text(size = 12), legend.key = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_blank())}
This is the code for my plot:
large_plot <- new_total_data[ , c("LPA.4class", "Total_Audit_Score", "HeavyDrinkDays", "SRE_heaviest", "AgeFirstDrink", "ln_K", "Urgency", "N_Factor", "Motor", "CTQ_TotalScore")]
large_plot <- na.omit(large_plot)
large_plot <- large_plot %>%
group_by(LPA.4class) %>%
summarise(across(everything(), mean, na.rm = TRUE)) %>%
as.data.frame(large_plot)
colnames(large_plot) <- c("LPA.4class", "AUDIT", "TLFB", "SRE", "LDH", "DD", "UPPSP", "NEO", "BIS", "CTQ")
rownames(large_plot) <- c("High Intensity", "Steady Rising", "Moderate Binge", "Early Binge w/ Fall")
large_plot <- subset(large_plot, select = -LPA.4class)
large_plot <- round(large_plot, digits = 0)
large_spider <- large_plot %>%
rownames_to_column(var = "group") %>%
as_tibble() %>%
tail(4) %>%
select(1:10)
img <- readJPEG("C:/Users/Jessica/Downloads/spider3.jpg")
ggspider(large_spider) +
labs(title = "Mean Scores by Binge Group") +
theme(plot.title = element_text(hjust = 0.5)) +
background_image(img)