library(ggforma)
library(ggplot2)
library(lattice)
library(grid)

clinics <- data.frame(
  Network = rep(c("North", "South"), each = 8),
  Coverage = c(69, 78, 85, 79, 89, 85, 82, 91, 53, 62, 64, 69, 72, 75, 77, 78),
  Testing = c(60, 62, 65, 70, 73, 65, 72, 75, 68, 70, 74, 75, 74, 82, 83, 89),
  Outreach = c(77, 83, 86, 82, 89, 88, 78, 85, 70, 76, 78, 72, 81, 77, 83, 81),
  Followup = c(62, 77, 75, 63, 85, 80, 73, 88, 54, 61, 58, 69, 67, 72, 70, 73)
)
profiles <- aggregate(. ~ Network, clinics, mean)
colours <- setNames(forma_palette(9)[c(6, 7)], c("North", "South"))
radar <- ggradar(profiles, vars = c(Coverage, Testing, Outreach, Followup),
                 group = Network, limits = c(0, 100), grid = "circle") +
  labs(title = "Average clinic profiles") +
  theme(panel.background = element_rect(fill = "#EEF6F4", colour = NA))

scatter <- xyplot(Followup ~ Coverage, data = clinics, aspect = "fill",
                  main = "Clinic scores · Lattice",
                  xlab = "Coverage score", ylab = "Follow-up score",
                  panel = function(x, y, subscripts, ...) {
                    grid.rect(gp = gpar(fill = "#F1F5FA", col = NA))
                    panel.xyplot(x, y, pch = 16, col = colours[clinics$Network[subscripts]], ...)
                  }, par.settings = list(axis.line = list(col = "transparent")))

figure <- ggplus(radar, scatter, nrow = 1, align = "panel", gap = 4)

# Enter figure in the R console to preview it.
render_example <- function(file, device = ragg::agg_png) {
  ggsave(file, figure, width = 150, height = 110, units = "mm",
         device = device, dpi = 144, bg = "white")
}

png.file <- file.path(tempdir(), "composition-radar-lattice.png")
render_example(png.file)
png.file
