Is there a way in ggplot2 to manipulate or customize the display of x axis text?

Challenge:I need to automatically (without hard coding) customize x axis text in my graph, as follows. This is exclusively for aesthetic appeal for my users:

  1. Two rows of text needed in place of x axis text. First row, displays a sub-category (Role) while the second row (breed) displays a main category. See reprex code data set below
  2. Vertical line separating the main categories

See attached plot where I had manually scribbled what I want to achieve.
Here is a reproducible example code.

require(tidyverse)
bark <- tribble(
  ~breed,    ~value,  ~role,    ~id,
  "Bulldog",   1,       "Dad",   "A",   
  "Bulldog",   1,       "Mom",   "B",
  "Bulldog",   2,       "Ch1",   "C",
  "Bulldog",   1,       "Ch2",   "D",
  "Bulldog",   2,       "Ch3",   "E",
  "Poodle",    1,       "Dad",   "F",
  "Poodle",    3,       "Mom",   "G",
  "Poodle",    1,       "Ch1",   "H",
  "Poodle",    4,       "Ch2",   "I",
  "Beagle",    1,       "Mom",   "J",
  "Beagle",    5,       "Ch1",   "K",
  "Beagle",    1,       "Ch2",   "L",
  "Pug",       1,       "Mom",   "M",
  "Pug",       2,       "Dad",   "N",
  "Pug",       3,       "Ch2",   "O",
  "Pug",       1,       "Ch3",   "P",
  "Boxer",     5,       "Dad",   "Q",
  "Boxer",     4,       "Ch1",   "R"
)
ggplot2::ggplot(data=bark,aes(x=id,y=value))+geom_point()+geom_text(aes(y=1,label=role))+theme(axis.title = element_blank(),axis.text.x = element_blank())

This is what I want to achieve

Yes you can do this quite easily. Using ggplot with facet_wrap, then put the facet labels at the bottom, and use factors for the x axis labels.

ggplot2::ggplot(data = bark) +
  geom_point(mapping = aes(x = role, y = value)) +
  facet_wrap("breed", nrow = 1, strip.position = "bottom")

You will need to fiddle around a bit to get the styling you want.

1 Like
library(tidyverse)

bark <- tribble(
    ~breed,    ~value,  ~role,    ~id,
    "Bulldog",   1,       "Dad",   "A",   
    "Bulldog",   1,       "Mom",   "B",
    "Bulldog",   2,       "Ch1",   "C",
    "Bulldog",   1,       "Ch2",   "D",
    "Bulldog",   2,       "Ch3",   "E",
    "Poodle",    1,       "Dad",   "F",
    "Poodle",    3,       "Mom",   "G",
    "Poodle",    1,       "Ch1",   "H",
    "Poodle",    4,       "Ch2",   "I",
    "Beagle",    1,       "Mom",   "J",
    "Beagle",    5,       "Ch1",   "K",
    "Beagle",    1,       "Ch2",   "L",
    "Pug",       1,       "Mom",   "M",
    "Pug",       2,       "Dad",   "N",
    "Pug",       3,       "Ch2",   "O",
    "Pug",       1,       "Ch3",   "P",
    "Boxer",     5,       "Dad",   "Q",
    "Boxer",     4,       "Ch1",   "R"
)

bark %>% 
    mutate(breed = fct_inorder(breed),
           role = factor(role, levels = c("Dad", "Mom", "Ch1", "Ch2", "Ch3"))) %>% 
    group_by(breed) %>% 
    complete(role) %>% 
    ggplot(aes(x = role, y = value)) + 
    geom_point() + 
    geom_text(aes(y = 0, label = role)) +
    facet_wrap(~breed, nrow = 1, strip.position = "bottom") +
    theme(axis.title = element_blank(),
          axis.text.x = element_blank())

Created on 2020-05-14 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.