1

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

Lisa B
  • 85
  • 6
  • Could you try this first: `#Data df1 <- df %>% mutate(name = glue(")"))%>% filter(Value!=0)` – Duck Sep 08 '20 at 12:50
  • And then `ggplot(df1,aes(x=Sample, y=Number)) +geom_point(aes(size=Value, alpha = 0.9)) +scale_y_discrete(labels= df1$name)` – Duck Sep 08 '20 at 12:50

1 Answers1

1

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())

mhovd
  • 3,724
  • 2
  • 21
  • 47
stefan
  • 90,330
  • 6
  • 25
  • 51
  • That's amazing, thank you! The only issue I have now is that the ordering is not quite right (I've had a lot of trouble with this). I've added a new image to the questions. Can you help? – Lisa B Sep 09 '20 at 06:28
  • Hi @LisaB. According to the new image you want the categories to be in descending order, right? This could be achieved e.g. by making use of `forcats::fct_rev`. Try `ggplot(aes(x = Sample, y = forcats::fct_rev(name))) + ...` – stefan Sep 09 '20 at 06:49
  • Thanks @stefan. That works everywhere except for one group where the order is 100,101,102,99. This is the issue I was having before, and I had to create the Numbers column to get them all in the right order (or assigning the names as factors with levels which was very time consuming). Any thoughts? – Lisa B Sep 09 '20 at 09:04
  • (: Always forget about the "1xx". In that case `ggplot(aes(x = Sample, y = forcats::fct_reorder(name, desc(as.numeric(stringr::str_extract(Comname, "^\\d+"))))))`. 1. str_extract extracts the number at the beginning of the string, 2. convert to numeric which 3. could be use inside fct_reorder to order the labels in descending order. – stefan Sep 09 '20 at 09:13
  • Perfect. Thank you so much. – Lisa B Sep 09 '20 at 09:34