0

I am producing a facet chart like this

suppressPackageStartupMessages({
  library(tidyverse)
  library(scales)
})

set.seed(123456)
test_df <-
  tibble(t=1:10,
         x=100+runif(10,-10,10),
         y=10+runif(10,-4,4),
         p=y/x)

test_df |>
  pivot_longer(cols=x:p,
               names_to = "var",
               values_to = "value") |>
  mutate(var=factor(var,levels=c("x","y","p"))) ->
test_df_long

test_df_long |>
  ggplot(aes(x=t,y=value)) +
  geom_line() +
  scale_y_continuous(limits=c(0,NA)) +
  facet_wrap(vars(var),ncol=1,
             scales = "free_y")

enter image description here

I would like to change the formatting of the labels for the p facet to percent.

I have not found a way to use different labelling functions to different facets.

Can it be done?

Peter
  • 11,500
  • 5
  • 21
  • 31
Erich Neuwirth
  • 943
  • 7
  • 13
  • Maybe take a look at [`facetscales`](https://github.com/zeehio/facetscales)? It seems that it can be done. – philiptomk May 10 '22 at 16:10
  • Does this address your question: [How can I change the Y-axis figures into percentages in a barplot?](https://stackoverflow.com/questions/27433798/how-can-i-change-the-y-axis-figures-into-percentages-in-a-barplot). If so, then this question is a duplicate. In short, `+ scale_y_continuous(labels = scales::percent)` – steveb May 10 '22 at 16:10
  • Does this answer your question? [How can I change the Y-axis figures into percentages in a barplot?](https://stackoverflow.com/questions/27433798/how-can-i-change-the-y-axis-figures-into-percentages-in-a-barplot) – steveb May 10 '22 at 16:14

2 Answers2

2

You could use ggh4x::facetted_pos_scales() to control position scales of individual facets. If you just need to adjust a single scale, you could use the formula notation as below. (Disclaimer: I'm the author of ggh4x)

suppressPackageStartupMessages({
  library(tidyverse)
  library(scales)
})

set.seed(123456)
test_df <-
  tibble(t=1:10,
         x=100+runif(10,-10,10),
         y=10+runif(10,-4,4),
         p=y/x)

test_df |>
  pivot_longer(cols=x:p,
               names_to = "var",
               values_to = "value") |>
  mutate(var=factor(var,levels=c("x","y","p"))) ->
  test_df_long

test_df_long |>
  ggplot(aes(x=t,y=value)) +
  geom_line() +
  scale_y_continuous(limits=c(0,NA)) +
  facet_wrap(vars(var),ncol=1,
             scales = "free_y") +
  ggh4x::facetted_pos_scales(
    y = var == "p" ~ scale_y_continuous(labels = percent_format())
  )

Created on 2022-05-11 by the reprex package (v2.0.1)

teunbrand
  • 33,645
  • 4
  • 37
  • 63
1

You can use the package facetscales like this:

devtools::install_github("zeehio/facetscales")
library(facetscales)

scales_y <- list(
  x = scale_y_continuous(),
  y = scale_y_continuous(),
  p = scale_y_continuous(labels = percent_format())
)

test_df_long |>
  ggplot(aes(x=t,y=value)) +
  geom_line() +
  scale_y_continuous(limits=c(0,NA)) +
  facet_grid_sc(rows = vars(var), scales = list(y = scales_y))

Output:

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53