Matthew Aldrovandi
Matthew Aldrovandi

Reputation: 49

How can I fiddle with the axis labels in a two axis bar chart

All I am trying to do is to get the bottom x-axis of the following bar chart to match the text in the legend. I got the top x-axis text to change by adding the "labels" argument to the "scale_x_discrete" part of the code. Here is what I have, big shout out to @Allan Cameron for helping me, try to ignore the colors as that is the next part of my code I will add:

silly

My data:

cat req app rej

BB 199 149 50

CF 20 12 8

CR 34 33 1

GM 50 33 17

LC 20 14 6

RC 61 50 11

W1 74 48 26

W2 56 42 14

Sorry I could not figure out how to attach a .csv.

And here is my code:

library(ggplot2)
library(tidyr)
library(dplyr)

pivot_longer(data, cols = c("req", "app", "rej")) %>%
  mutate(name = factor(name, levels = c("req", "app","rej"))) %>%
  ggplot(aes(name, value, fill = cat)) +
  labs(x="Study Category", y="Number of Studies") +
  geom_col() +
  facet_grid(~cat, switch = "x") +
  scale_x_discrete(expand = c(0.5, 0.5), labels=c("Requested", "Approved", "Rejected")) +
  theme_classic() +
  theme(panel.spacing = unit(0, "points"),
        strip.placement = "outside",
        strip.background = element_blank()) + 
  theme(axis.text.x= element_text(size=6), legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black")) + 
  theme(legend.title.align=0.5) + 
  labs(fill = "Study Category") + 
  scale_fill_discrete(labels = c("Biota and Biodiversity", "Connectivity and Fragmentation", "Cultural Resources","Geomorphology","Landscape and Land Cover","Recreation","Water Quality","Water Quantity"))

I have tried this: Add secondary X axis labels to ggplot with one X axis

but the breaks arguments did not work for me. I have tried adding labels all over the place but the first label argument I mentioned at the top overrides everything.

Upvotes: 0

Views: 48

Answers (1)

Duck
Duck

Reputation: 39595

I would suggest next approach by formating your variable inside mutate(). In that way you could optimize the fill option directly using the content of the variable. It is a trick I learnt from same @AllanCameron:

library(ggplot2)
library(tidyr)
library(dplyr)

pivot_longer(data, cols = c("req", "app", "rej")) %>%
  mutate(name = factor(name, levels = c("req", "app","rej")),
         cat = factor(cat, levels = c("BB","CF","CR","GM","LC","RC","W1","W2"),
                      labels = c("Biota and Biodiversity",
                                      "Connectivity and Fragmentation",
                                      "Cultural Resources",
                                      "Geomorphology",
                                      "Landscape and Land Cover",
                                      "Recreation",
                                      "Water Quality",
                                      "Water Quantity"))) %>%
  ggplot(aes(name, value, fill = cat)) +
  labs(x="Study Category", y="Number of Studies") +
  geom_col() +
  facet_grid(~cat, switch = "x") +
  scale_x_discrete(expand = c(0.5, 0.5), labels=c("Requested", "Approved", "Rejected")) +
  theme_classic() +
  theme(panel.spacing = unit(0, "points"),
        strip.placement = "outside",
        strip.background = element_blank()) + 
  theme(axis.text.x= element_text(size=6), legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black")) + 
  theme(legend.title.align=0.5) + 
  labs(fill = "Study Category")

Output:

enter image description here

Upvotes: 1

Related Questions