Using plotmath for ggplot annotations

  • Using plotmath for ggplot annotations

     David updated 3 months, 1 week ago 2 Members · 6 Posts
  • jordan.trachtenberg

    Member
    January 12, 2021 at 8:56 am

    I’m not sure if there’s an easier way to do this, but I was looking for ways to emphasize annotate() text in ggplot using either colors or text formats (bold, italic, etc). I found some posts about using the plotmath functions in grDevices, but I was wondering if there was an easier way to do this.

    Here is my code:

    # First, make a dumbbell plot of all schools

    change_over_time %>%

    filter(race_ethnicity == "Black/African American") %>%

    mutate(percent_students_display = percent(percent_students, accuracy = 1)) %>% # 1 - nearest whole number, 0.1 - 1 decimal place, but this doesn't work because it converts the data to chr format. However, it would work for labeling text on a plot.

    pivot_wider(id_cols = c(district_id, district_name, percent_change),

    names_from = year,

    values_from = percent_students) %>%

    clean_names() %>%

    slice(1:20) %>%

    ungroup() %>%

    mutate(district_name = reorder(district_name, desc(percent_change))) %>%

    ggplot(aes(x = x2017_18,

    xend = x2018_19,

    y = district_name,

    yend = district_name)) +

    ggalt::geom_dumbbell(inherit.aes = TRUE,

    colour_x = "gray",

    colour = "gray",

    colour_xend = "#800000",

    size_x = 1.5,

    size_xend = 3) +

    theme_rhit() +

    labs(title = "These 20 districts experienced attrition of African American students
    from 2017-18 to 2018-19",

    caption = "test caption: highlighted population sizes may be small",

    family = "Segoe_UI") +

    scale_x_continuous(limits = c(0, 0.22),

    label = percent_format(accuracy = 1)) +

    annotate("text",

    x = .12,

    y = 3,

    family = "Segoe_UI",

    label = expression(phantom("Red dots ") * "represent 2018-19."),

    linheight = 1,

    color = "#A0A0A0",

    hjust = 0,

    yjust = 0) +

    annotate("text",

    x = .12,

    y = 2.1,

    family = "Segoe_UI",

    label = "Gray dots represent 2017-18.",

    linheight = 1,

    color = "#A0A0A0",

    hjust = 0,

    yjust = 0) +

    annotate("text",

    x = .12,

    y = 3,

    family = "Segoe_UI",

    label = expression("Red dots " * phantom("represent 2018-19.")), # expression() and phamntom() are from the grDevices package under plotmath: https://blog.revolutionanalytics.com/2009/01/multicolor-text-in-r.html # this other approach did not work: https://andrewwhitby.com/2017/09/18/multi-color-text-ggplot2/

    linheight = 1,

    color = "#800000",

    hjust = 0,

    yjust = 0) +

    annotate("text",

    x = .14,

    y = 19,

    family = "Segoe_UI",

    label = "atop(bold('Southern Oregon ESD ')~had~the~highest~attrition~of, 'African American students in 2018-19')", # atop() and bold() are from the grDevices package under plotmath. https://stackoverflow.com/questions/31568453/using-different-font-styles-in-annotate-ggplot2

    linheight = 1,

    color = "#A0A0A0",

    parse = TRUE)

    And I’ve attached a screenshot of my resulting plot. Thanks!

  • jordan.trachtenberg

    Member
    January 12, 2021 at 9:03 am

    To clarify, I like that it is fairly easy to modify the plot title using HTML, but with annotate(), I have to make a new layer for every text modification. Is there a similar way to modify text in annotate() using HTML or other easier methods than plotmath?

  • David

    Organizer
    January 12, 2021 at 9:53 am

    Have you tried geom_textbox() from ggtext? I think it would work here, but tell me if I’ve missed something.

    • jordan.trachtenberg

      Member
      January 12, 2021 at 10:22 am

      Thanks, David, for pointing me in the right direction. Looking at ggtext, it actually seems like geom_richtext() is what I want because it allows you to change text colors within the label as well.

    • jordan.trachtenberg

      Member
      January 12, 2021 at 12:31 pm

      My solution:

      change_over_time %>%

      filter(race_ethnicity == "Black/African American") %>%

      mutate(percent_students_display = percent(percent_students, accuracy = 1)) %>% # 1 - nearest whole number, 0.1 - 1 decimal place, but this doesn't work because it converts the data to chr format. However, it would work for labeling text on a plot.

      pivot_wider(id_cols = c(district_id, district_name, percent_change),

      names_from = year,

      values_from = percent_students) %>%

      clean_names() %>%

      slice(1:20) %>%

      ungroup() %>%

      mutate(district_name = reorder(district_name, desc(percent_change))) %>%

      ggplot(aes(x = x2017_18,

      xend = x2018_19,

      y = district_name,

      yend = district_name)) +

      ggalt::geom_dumbbell(inherit.aes = TRUE,

      colour_x = "gray",

      colour = "gray",

      colour_xend = "#800000",

      size_x = 1.5,

      size_xend = 3) +

      geom_richtext(aes(x = 0.07,

      y = 18.5,

      label = "Southern Oregon ESD had the highest % attrition
      of African American students in 2018-19')",

      hjust = 0,

      vjust = 0),

      colour = "gray",

      fill = NA, label.color = NA, # remove background and outline

      label.padding = grid::unit(rep(0, 4), "pt"),

      family = "Segoe_UI") + # remove padding

      geom_richtext(aes(x = 0.12,

      y = 3,

      label = "Red dots represent 2018-19.
      Gray dots represent 2017-18.",

      hjust = 0,

      vjust = 0),

      colour = "gray",

      fill = NA, label.color = NA, # remove background and outline

      label.padding = grid::unit(rep(0, 4), "pt"),

      family = "Segoe_UI") + # remove padding

      theme_rhit() +

      labs(title = "These 20 districts experienced attrition of African American students
      from 2017-18 to 2018-19",

      caption = "test caption: highlighted population sizes may be small",

      family = "Segoe_UI") +

      scale_x_continuous(limits = c(0, 0.22),

      label = percent_format(accuracy = 1))

  • David

    Organizer
    January 12, 2021 at 2:21 pm

    Glad you got it figured out!

Log in to reply.

Original Post
0 of 0 posts June 2018
Now