0

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!

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • 1
    Where is the `setorder` function from - data.table? – Jon Spring Aug 15 '21 at 15:51
  • I don't know a straightforward way to plot geom_ribbon between two crossing series without doing an interpolation step to mark the crosses: https://stackoverflow.com/a/45247346/6851825 – Jon Spring Aug 15 '21 at 16:01
  • Yeesh. Somewhat unsurprisingly, this got really complicated really quickly. I might be able to get that to work, but the graph in that question is again using two separate geom_line commands instead of geom_point like I'm using. They then reference the two separate geom_line within the code for the two geom_ribbon commands. However, since I'm using a single geom_point, I'm not sure how to refer to one line or the other (i.e. one group) or the other when putting together the geom_ribbon commands. Do you know of a way to do that? Should I make it a separate SO question? – Aaron Siebenga Aug 17 '21 at 10:57

0 Answers0