0

I'd wondering if someone could please offer me a suggestion on how to sort out my shaded error for each line in this radar plot? I've tried several different approaches but am not getting what I want.

library(ggplot2)
library(scales)

# Make some data
Group.no    <- 3
Group.names <- c("1","2","3")
Metric.no   <- 4
Metriclist  <- c("M1", "M4", "M6","M8")

Metric  <- c(rep(c(Metriclist), each = Group.no))
Group   <- c(rep(c(Group.names), times = Metric.no))

Mg    <- c(87.7, 93.8, 72.5, 190.3, 170.9, 138.4, 283.2, 248.7, 196.5, 340.6, 307.9, 240.9)

d <- data.frame(Metric, Group, Mg)
d$lowCI  <- Mg-8
d$highCI <- Mg+8 

# Plot data
Plot <- ggplot(d, aes(x = Metric, y = Mg, group = Group)) +
  geom_polygon(aes(group = Group, colour = Group), fill = NA, size = 1.1) +  
  geom_ribbon(aes(x=Metric,y=Mg,ymin=lowCI,ymax=highCI, group = Group, fill=Group), alpha=.3) +
  coord_polar(start = -((180/Metric.no)*(pi/180)))+
  theme_light()

Plot

Example plot

As you can see, the geom_ribbon isnt plotting the lower and upper CI's in line with geom_polgyon. I am currently at the peak of my knowledge on this. Is anyone able to offer a suitable fix so that the shaded CI's track the polygon please so that both are straight lines?

Thanks in advance for any solutions!

Maylor
  • 3
  • 2
  • Can you clarify what you would like to be different? Do you want the polygon curved like the ribbons, the ribbons straight like the polygons, or something else? – Miff Mar 23 '21 at 12:05
  • @Miff sorry if this wasn't clear. it's for the ribbons to be straight like the polygons are. the straight lines are correct, but the geom_ribbon is curved when I want it straight aswell – Maylor Mar 23 '21 at 12:15
  • Possible duplicate: https://stackoverflow.com/questions/42562128/ggplot2-connecting-points-in-polar-coordinates-with-a-straight-line-2 – teunbrand Mar 23 '21 at 14:29
  • Also related: https://stackoverflow.com/questions/66196451/draw-straight-line-between-any-two-point-when-using-coord-polar-in-ggplot2-r/66196752 and https://stackoverflow.com/questions/57209060/how-to-draw-a-radar-plot-in-ggplot-using-polar-coordinates/57209765 – teunbrand Mar 23 '21 at 14:30

1 Answers1

0

With a bit of fiddling with the data, you can just about make it work. Create a separate dataframe that has the confidence intervals (note that we need to repeat the first point to make the intervals work correctly between M8 and M1) with:

i <- c(seq_along(d[[1]]), which(d$Metric=="M1"))

dCI <- rbind(data.frame(d[i,1:2], CI=d$lowCI[i], type="low") ,
             data.frame(d[i,1:2], CI=d$highCI[i], type="high"))

Then we can add the confidence intervals as a polygon with:

Plot <- ggplot(d, aes(x = Metric, y = Mg, group = Group)) +
  geom_polygon(aes(group = Group, colour = Group), fill = NA, size = 1.1) +  
  geom_polygon(aes(x=Metric,y=CI, group = Group, fill=Group), data=dCI, alpha=.3) +
  coord_polar(start = -((180/Metric.no)*(pi/180)))+
  theme_light()

Plot

This produces the output:

Sample output

Miff
  • 7,486
  • 20
  • 20
  • Thanks so much @Miff, I had tried adding geom_polygon twice with the fill imputed, but it hadn't crossed my mind to use a different df for this - This works perfectly. Thanks! – Maylor Mar 23 '21 at 15:18