First question I've asked on StackOverflow, so apologies if I make any mistakes.
I have several polar graphs showing scores from subjects in 2 case conditions, with the graph ordered by their score in the first case. This is done via a ggplot buried inside several for loops, with the loops determining which rows and columns from the source dataframe the graph uses.
I've been asked to have the graphs automatically shade in the area between the two cases for each subject, and ideally colour-coded such that you can easily tell which case has the higher score. Here is the graph the Reprex produces with the shaded areas drawn on to show what I'm trying to do.
Looking around brought me to this thread and geom_ribbon, but I was unable to make it work as this previous case has the two lines as separate objects while both my lines come from the same source table. In addition, geom_ribbon seems to fail to fill in the area between the first and last subjects on polar graphs. However, I'm still a novice R user so geom_ribbon could still be the answer, assuming there is a way to do this without restructuring the incoming data.
Any suggestions or ideas regarding these issues would be greatly appreciated. See below for my Reprex.
library(tidyverse)
library(ggplot2)
library(dplyr)
library(gdata)
# Create data for both cases #
set.seed(1)
Subject <- rep(1:20)
Case1 <- as.data.frame(cbind(Subject,"1", runif(20, min=0, max=80)))
Case1$V3 <- as.numeric(Case1$V3)
Case2 <- as.data.frame(cbind(Subject,"2", runif(20, min=0, max=80)))
Case2$V3 <- as.numeric(Case2$V3)
# Sort by Case 1 and combine both cases into one dataframe #
SubjectOrder <- unique(setorder(Case1,V3)[,1])
Case1 <- Case1[match(SubjectOrder, Case1$Subject),]
Case2 <- Case2[match(SubjectOrder, Case2$Subject),]
Data <- interleave(Case1,Case2)
colnames(Data) <- c("Subject","Case","Score")
Data$Subject <- as.numeric(Data$Subject)
Data$Score <- as.numeric(Data$Score)
# Graphing #
print(ggplot(data=Data, aes(x=factor(Subject,level=SubjectOrder),y=Score,group=Case,shape=Case,color=Case)) +
geom_point(stat='identity') +
geom_polygon(fill=NA) +
coord_polar(start = 0) +
theme_bw() +
scale_color_hue(direction = -1, h.start = 90) +
scale_shape_manual(labels=c("1","2"),values=c(24,21)) +
ylim(0,80) +
labs(x=NULL))
Thanks!