rggplot2markdownggtext

ggplot2 Markdown in theme elements. Combining two columns to make one tick label with different fonts


I am trying to apply this solution https://github.com/wilkelab/ggtext to my data to create tick labels from a combination of two columns and apply different fonts to each. I think I've adapted the example code to my scenario but am getting this error after the mutate stage:

Error in check_breaks_labels(breaks, labels) : object 'name' not found

Any pointers appreciated.

I'm looking for something like the below.

![Looking for something like the below.

Here's the code I've tried:

df$Colour <- as.factor(df$Colour)
df$Number <- as.factor(df$Number)
df$Family <- factor(df$Family, levels = c("Ardeidae"))

df %>% mutate(
  name = glue("<i style={Comname} ({Sciname}</i>)"))%>%

ggplot(df[df$Value != 0,], aes(x=Sample, y=Number)) +
  geom_point(aes(size=Value, alpha = 0.9)) +
  scale_y_discrete(labels= name) +
  facet_grid(Family ~ Year, scales = "free", space = "free") 

And some sample data.

df <- structure(list(Year = c("1984 - 1989", "2017 - 2020", "1984 - 1989", 
"2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", 
"1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", 
"2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", 
"1984 - 1989", "2017 - 2020", "1984 - 1989", "2017 - 2020", "1984 - 1989", 
"2017 - 2020"), Sample = c("Developed_zone_1992", "Developed_zone_2020", 
"Paddock_zone_1992", "Paddock_zone_2020", "Sanctuary_zone_1992", 
"Sanctuary_zone_2020", "Developed_zone_1992", "Developed_zone_2020", 
"Paddock_zone_1992", "Paddock_zone_2020", "Sanctuary_zone_1992", 
"Sanctuary_zone_2020", "Developed_zone_1992", "Developed_zone_2020", 
"Paddock_zone_1992", "Paddock_zone_2020", "Sanctuary_zone_1992", 
"Sanctuary_zone_2020", "Developed_zone_1992", "Developed_zone_2020", 
"Paddock_zone_1992", "Paddock_zone_2020", "Sanctuary_zone_1992", 
"Sanctuary_zone_2020"), Colour = structure(c(1L, 1L, 2L, 2L, 
3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 
2L, 2L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor"), 
    Value = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 
    2L, 0L, 0L, 1L, 0L, 0L, 10L, 1L, 5L, 5L, 0L, 5L), Family = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Ardeidae", class = "factor"), 
    Comname = c("47. Little egret", "47. Little egret", "47. Little egret", 
    "47. Little egret", "47. Little egret", "47. Little egret", 
    "46. Western great egret", "46. Western great egret", "46. Western great egret", 
    "46. Western great egret", "46. Western great egret", "46. Western great egret", 
    "45. Purple heron", "45. Purple heron", "45. Purple heron", 
    "45. Purple heron", "45. Purple heron", "45. Purple heron", 
    "44. Grey heron", "44. Grey heron", "44. Grey heron", "44. Grey heron", 
    "44. Grey heron", "44. Grey heron"), Sciname = c("Egretta garzetta", 
    "Egretta garzetta", "Egretta garzetta", "Egretta garzetta", 
    "Egretta garzetta", "Egretta garzetta", "Ardea alba", "Ardea alba", 
    "Ardea alba", "Ardea alba", "Ardea alba", "Ardea alba", "Ardea purpurea", 
    "Ardea purpurea", "Ardea purpurea", "Ardea purpurea", "Ardea purpurea", 
    "Ardea purpurea", "Ardea cinerea", "Ardea cinerea", "Ardea cinerea", 
    "Ardea cinerea", "Ardea cinerea", "Ardea cinerea"), Number = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
    3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("96", "97", "98", 
    "99"), class = "factor")), row.names = c(NA, -24L), class = "data.frame")

enter image description here

Update


Solution

  • As already pointed out by @Duck there are some minor issues in your code. Unfortunately these will not solve the ggtext issue

    1. When you want to make use of markdown elements in ggtext you have to make use of element_markdown by setting e.g. axis.text.y = element_markdown() inside theme().

    2. To make the styling work you have to escape the point . in your Comname by the HTML entity &#46;. (The reason is that a number followed by a . marks the start of an ordered list in markdown.)

    3. After these changes you don't need the scale_y_discrete. ggplot2 will by default use name to label the ticks

    df$Colour <- as.factor(df$Colour)
    df$Number <- as.factor(df$Number)
    df$Family <- factor(df$Family, levels = c("Ardeidae"))
    
    library(dplyr)
    library(glue)
    library(ggplot2)
    library(ggtext)
    
    df1 <- df %>%
      mutate(
        Comname = stringr::str_replace(Comname, "\\.", "&#46;"),
        name = glue::glue("{Comname} <i>{Sciname}</i>")
      ) %>%
      filter(Value != 0) 
    
    df1 %>% 
      ggplot(aes(x = Sample, y = name)) +
      geom_point(aes(size = Value, alpha = 0.9)) +
      facet_grid(Family ~ Year, scales = "free", space = "free") +
      theme(axis.text.y = element_markdown())