0

I want to show all facets on the one plot if possible, even if one of the facets has a very different scale.

Dataset looks like this:

dat <- structure(list(u_measure = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L), type = c("FT", "OE", "OV_SAT", "SALARY", "FT", 
"OE", "OV_SAT", "SALARY", "FT", "OE", "OV_SAT", "SALARY"), Syd_flag = c("Syd", 
"Syd", "Syd", "Syd", "Syd", "Syd", "Syd", "Syd", "MU", "MU", 
"MU", "MU"), measure = c(80L, 75L, 90L, 65000L, 80L, 65L, 70L, 
70000L, 70L, 55L, 50L, 75000L)), class = "data.frame", row.names = c(NA, 
-12L))

One of the facets is a salary, the scale is really different but want to show all on the one plot.

**edited question to clarify that I need the first 3 facets to be the same at 50 - 100 so they can be visually compared **

ggplot code as follows:

ggplot(data = dat, aes(x = reorder(u_measure, -measure), 
             y = measure, fill = Syd_flag)) +
  geom_bar(stat = "identity") +
  facet_nested(. ~ type, 
               scales = "free", 
               labeller = label_wrap_gen(width = 25, multi_line = TRUE)) +
  ggh4x::facetted_pos_scales(y  = list(
    type != "SALARY" ~ scale_y_continuous(limits = c(50,100)),
    type == "SALARY" ~ scale_y_continuous(limits = c(65000,75000))))+
  geom_text(
    aes(y = measure,
        label = format(round(measure, 0))), 
    size = 3.5, hjust = 1.5, vjust = 0.5, angle = 90) +
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_markdown(angle = 90, size = 11,
                                   hjust = 0, vjust = 0.5),
    axis.text.y = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    legend.position = "none",
    plot.background = element_rect(colour = NA)
  )

I cant get the scales to show the salary variable properly. You can see from the graph I cant get the salary facet right. Any ideas?

enter image description here

Keelin
  • 367
  • 1
  • 10

2 Answers2

0

I think in this case you don't need ggh4x package here:

Use facet_wrap and scales = "free":

library(ggplot2)

ggplot(data = dat, aes(x = reorder(u_measure, -measure), 
                       y = measure, fill = Syd_flag)) +
  geom_bar(stat = "identity") +
  facet_wrap(~ type, scales = "free") +
  geom_text(aes(label = format(round(measure, 0))),
            size = 3.5, hjust = 1.5, vjust = 0.5, angle = 90) +
  theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_text(angle = 90, size = 11, hjust = 0, vjust = 0.5),
    axis.text.y = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    legend.position = "none",
    plot.background = element_rect(colour = NA)
  )

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
0

The following code from ggh4x package fixes the issue. Using facet_grid2 with independent y axes and then setting the custom scales works.

  ggh4x::facet_grid2(. ~ indicator, scales = "free", independent = "y") +
  #set custom scales
  ggh4x::facetted_pos_scales(y  = list(
      indicator != "SALARY" ~ scale_y_continuous(limits = c(50,100)),
      indicator == "SALARY" ~ scale_y_continuous(limits = c(60000,70000))))
Keelin
  • 367
  • 1
  • 10