rggplot2labelwindrose

How to get angled labels of wind rose diagram in R using ggplot2?


I would like to plot a seperate legend to my wind rose diagram where the labels are angled in accordance with the 18 categories in the plot. Is that possible with ggplot?

Example of what output would look like: (https://i.sstatic.net/hJOf0.jpg)

Example code below gives me labels that are horisontal (and also expands outside of the plot are). (https://i.sstatic.net/I31LY.jpg)


library(ggplot2)
library(RColorBrewer)
library(dplyr)

nb.cols <- 18
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)

df <- data.frame(x=1, y=1:18)
NCP <- c("Maintenance of options", "Food and feed", "Materials, companionship and labor", "Medicinaland genetic resources", "Energy",                                  
"Physical and psychological experiences",  "Supporting identities", "Learning and inspiration", "Formation of soils and sediments",        
"Regulation of hazards and extreme events", "Regulation of freshwater quantity", "Regulation of freshwater quality", "Regulation of climate",                   
"Habitat creation and maintenance", "Regulation of biological processes", "Pollination and dispersal of seeds",      
"Regulation of air quality", "Regulation of ocean acidification")    
df <- cbind(df, as.data.frame(NCP))

#plot for individual region
ggplot(df,
       aes(x = NCP, y = y)) +
  geom_col(width = 1, fill = mycolors) +
  coord_polar(start = 0) + # change start value if you want a different orientation
  theme_void() +
  theme(axis.title = element_blank(),
        panel.ontop = FALSE, # change to FALSE for grid lines below the wind rose
        panel.background = element_blank())

#Plot for labels
ggplot(df,
       aes(x = NCP, y = x)) +
  geom_col(width = 1, fill = mycolors) +
  coord_polar(start = 0) + # change start value if you want a different orientation
  theme(panel.grid.major = element_blank(), 
        axis.line = element_blank(), 
        plot.background = element_blank(),
        panel.background = element_blank(), 
        axis.title = element_blank(), 
        axis.ticks.y = element_blank(),
        axis.text.y=element_blank(),
        axis.text.x = element_text(colour = "Black", size= 6),
        panel.ontop = FALSE)

I've tried adjust the different axis.text.x elemements but I can't seem to get it angeled.


Solution

  • Perhaps there is a more straightforward approach but one option would be to use geomtextpath::geom_textsegment to add your labels on top of the bars:

    library(ggplot2)
    library(RColorBrewer)
    library(geomtextpath)
    
    nb.cols <- 18
    mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)
    
    df <- data.frame(x = 1, y = 1:18)
    NCP <- c(
      "Maintenance of options", "Food and feed", "Materials, companionship and labor", "Medicinaland genetic resources", "Energy",
      "Physical and psychological experiences", "Supporting identities", "Learning and inspiration", "Formation of soils and sediments",
      "Regulation of hazards and extreme events", "Regulation of freshwater quantity", "Regulation of freshwater quality", "Regulation of climate",
      "Habitat creation and maintenance", "Regulation of biological processes", "Pollination and dispersal of seeds",
      "Regulation of air quality", "Regulation of ocean acidification"
    )
    df <- cbind(df, as.data.frame(NCP))
    mycolors <- setNames(mycolors, NCP)
    
    ggplot(
      df,
      aes(x = NCP, y = x)
    ) +
      geom_textsegment(aes(label = NCP, xend = NCP, y = 1.05, yend = Inf),
        hjust = 0, color = NA, textcolour = "black", size = 6 / .pt
      ) +
      geom_col(aes(fill = NCP), width = 1) +
      scale_fill_manual(values = mycolors, guide = "none") +
      theme(
        panel.grid.major = element_blank(),
        axis.line = element_blank(),
        plot.background = element_blank(),
        panel.background = element_blank(),
        axis.title = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text = element_blank(),
        panel.ontop = FALSE
      ) +
      ylim(c(0, 1.5)) +
      coord_polar(start = 0)
    

    enter image description here