where to pass argument to radiate margin labels of a polar heatmap

Viewed 23

This question builds on from here: Drawing a polar heatmap

> dput(names.d)
c("0050773", "0050774", "0050775", "0050776", "0050777", "0050778", 
"0050779", "0050780", "0050781", "0050782", "0050783", "0050784", 
"0050785", "0050786", "0050787", "0050788", "0050789", "0050790", 
"0050808", "0050809", "0050810", "0050811", "0050812", "0050813", 
"0050814", "0050818", "0050819", "0050820", "0050821", "0050822"
)

Based on this, I have come up with the following code:

set.seed(20220913)
arr <- matrix(runif(15*30), nrow = 30)
dff <- as.data.frame(arr)
names(dff) <- paste(sample(letters, replace = F), sample(letters, replace = F), sep = " ")[1:15]

library(tidyverse)
   
dff %>%
    mutate(Site = seq(nrow(.))) %>%
    pivot_longer(-Site, names_to = 'Species', values_to = 'Abundance') %>%
    mutate(yval = match(Species, colnames(dff))) %>%
    ggplot(aes(Site, yval, fill = Abundance)) +
    geom_tile(color = "black") +
    geom_text(aes(label = colnames(dff)), hjust = 1.1, size = 3,
              data = data.frame(Site = 31.5, yval = 1:15, Abundance = 1)) +
    coord_polar() +
    scale_y_continuous(limits = c(-5, 15.5)) +
    scale_x_continuous(limits = c(0.5, 31.5), breaks = 1:30, labels = names.d,
                       name = 'Breeding site') +
    scale_fill_gradientn(colors = colorRampPalette(RColorBrewer::brewer.pal(name = "YlOrRd", n = 9))(25), values = 0:1, labels = scales::percent)+
    theme_void(base_size = 16) +
    theme(axis.text.x = element_text(size = 12),
          axis.title.x = element_text())

which gives me the following figure: enter image description here

Which is great, but I would like the labels on the rim of the figure to radiate out (or be tangent, for that matter). So, I wrote the angles as:

ang <- 1:30/31.5*360

However, I can not see where to pass this argument. Looking around, it would normally be in the aes function, but there the labels are for the y-axis in the figure (before being changed to the polar coordinates), and what I am wanting rotated should be in the x-axis. So, how do I do this? Thanks for any suggestions!

1 Answers

You can add this in the axis.text.x = element_text() :

ang <- 90 - (1:30/31.5*360)

dff %>%
  mutate(Site = seq(nrow(.))) %>%
  pivot_longer(-Site, names_to = 'Species', values_to = 'Abundance') %>%
  mutate(yval = match(Species, colnames(dff))) %>%
  ggplot(aes(Site, yval, fill = Abundance)) +
  geom_tile(color = "black") +
  geom_text(aes(label = colnames(dff)), hjust = 1.1, size = 3,
            data = data.frame(Site = 31.5, yval = 1:15, Abundance = 1)) +
  coord_polar() +
  scale_y_continuous(limits = c(-5, 15.5)) +
  scale_x_continuous(limits = c(0.5, 31.5), breaks = 1:30, labels = names.d,
                     name = 'Breeding site') +
  scale_fill_gradientn(colors = colorRampPalette(RColorBrewer::brewer.pal(name = "YlOrRd", n = 9))(25), values = 0:1, labels = scales::percent)+
  theme_void(base_size = 16) +
  theme(axis.text.x = element_text(size = 12, angle = ang),
        axis.title.x = element_text())

enter image description here

Related