Ben Carlson
Ben Carlson

Reputation: 1265

ggradar: use group plus facet_wrap at the same time in

I'm using the ggradar package which makes very nice radar plots. I can use it to create one radar plot with multiple lines using a column called "group". Or, I can create multiple radar facets with a single line. But, I can't have facets with multiple lines (groups). Is this possible?

Note I'm using ggradar (https://github.com/ricardo-bion/ggradar) and not ggiraphExtra::ggRadar

library(ggradar)
library(scales)

set.seed(123)
gdat <- tibble(
  facet=c('A','A','B','B'),
  group=c(2010,2020,2010,2020),
  var1=rescale(runif(4)),
  var2=rescale(runif(4)),
  var3=rescale(runif(4)))

What I would like to have is two panels (A and B), each with two lines (2010, 2020). The following does not work.

gdat %>%
ggradar() +
  facet_wrap(vars(facet))

NULL

However, if I only select one "facet" I can use group to plot 2010, 2020 on the same plot.

gdat %>%
  filter(facet=='A') %>%
  select(-facet) %>%
  ggradar()

One facet two lines

Or, I can have multiple facets, but each only has one line

gdat %>%
  filter(group==2010) %>%
  mutate(group=facet) %>% #need to rename "facet" to "group"
  select(-facet) %>%
  ggradar() +
    facet_wrap(vars(group))

Two facets one line

Is it possible to have two facets two lines?

Upvotes: 4

Views: 1065

Answers (1)

L Tyrone
L Tyrone

Reputation: 7065

You can use the patchwork package to achieve your desired outcome:

devtools::install_github("ricardo-bion/ggradar", dependencies = TRUE)
library(scales)
library(dplyr)
library(tibble)
library(ggradar)
library(ggplot2)
library(patchwork)

set.seed(123)

gdat <- tibble(
  facet=c("A","A","B","B"),
  group=c(2010,2020,2010,2020),
  var1=rescale(runif(4)),
  var2=rescale(runif(4)),
  var3=rescale(runif(4)))

plot1 <- gdat |>
  filter(facet == "A") |>
  select(-facet) |>
  ggradar(plot.legend = FALSE,
          axis.label.offset = 1.2,
          axis.label.size = 4,
          group.point.size = 3,
          group.line.width = 1,
          plot.title = "Group A") +
  theme(plot.title = element_text(hjust = .5, size = 12))

plot2 <- gdat |>
  filter(facet == "B") |>
  select(-facet) |>
  ggradar(axis.label.offset = 1.2,
          axis.label.size = 4,
          group.point.size = 3,
          group.line.width = 1,
          plot.title = "Group B") +
  theme(plot.title = element_text(hjust = .5, size = 12))

# Patchwork plots together
plot1 + plot2

result

Upvotes: 0

Related Questions