Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change y axis individually in facet plots

I have this data containing 3 components and the total of all components put together and I am trying to plot everything in one figure, so I decided to use a facet plot but I would like to plot the components all using the same y axis range, but since the total is a much larger number I want to set its axis individually but I don't seem to be able to do it

I tried:

library(tidyverse)
library(ggplot2)
library(dplyr)
set.seed(123)
data <- tibble(
 Treatment = rep(c("Control", "Treatment"), each = 4),
 Year = rep(2021:2024, times = 2),
 a = runif(8, 100, 150),
 b = runif(8, 100, 150),
 c = runif(8, 100, 150),
)

data <- data %>%
 mutate(Total = a + b+ c)

data_long <- data %>%
 pivot_longer(cols = -c(Year, Treatment), names_to = "Component", values_to = "Value") 

plot <- ggplot(data_long, aes(x = as.factor(Year), y = Value, group = Treatment)) +
 geom_line(aes(color = Treatment), size = 1) +
 geom_point(aes(color = Treatment, shape = Treatment), size = 3) +
 scale_y_continuous(limits = c(0, 200)) +
 facet_wrap(~Component, scales = "fixed", ncol = 4) +
 scale_color_manual(values = c("Control" = "blue", "Treatment" = "red")) 

But this code fixed the axis on all to 0-200, so total is not visible in the plot.

I then tried replacing face_wrap and scale_y_continouos for this

facet_wrap(~Component, scales = "free_y", ncol = 4) +
scale_y_continuous(
    limits = c(0, ifelse(any(data_long$Component == "Total"), 500, 200)))

But that didn't give any result. I know you can remove the scale_y_continuous argument and use "fixed" and "free_y," but there has to be a way to change the scale on one of the plots.

like image 330
Gabriel Avatar asked Oct 24 '25 14:10

Gabriel


1 Answers

Perhaps try building two plots (i.e. a, b, c in one, and Total in the other) then joining them together using the patchwork package, e.g.

library(tidyverse)

set.seed(123)
data <- tibble(
  Treatment = rep(c("Control", "Treatment"), each = 4),
  Year = rep(2021:2024, times = 2),
  a = runif(8, 100, 150),
  b = runif(8, 100, 150),
  c = runif(8, 100, 150),
)

data <- data %>%
  mutate(Total = a + b+ c)

data_long <- data %>%
  pivot_longer(cols = -c(Year, Treatment), names_to = "Component", values_to = "Value") 

a <- data_long %>%
  filter(Component != "Total") %>%
  ggplot(aes(x = as.factor(Year), y = Value, group = Treatment)) +
  geom_line(aes(color = Treatment), size = 1) +
  geom_point(aes(color = Treatment, shape = Treatment), size = 3) +
  scale_y_continuous(limits = c(0, 200)) +
  facet_wrap(~Component, scales = "free_x", ncol = 4) +
  scale_color_manual(values = c("Control" = "blue", "Treatment" = "red"))

b <- data_long %>%
  filter(Component == "Total") %>%
  ggplot(aes(x = as.factor(Year), y = Value, group = Treatment)) +
  geom_line(aes(color = Treatment), size = 1) +
  geom_point(aes(color = Treatment, shape = Treatment), size = 3) +
  scale_y_continuous(limits = c(0, 500)) +
  facet_wrap(~Component, scales = "free_x", ncol = 4) +
  scale_color_manual(values = c("Control" = "blue", "Treatment" = "red"))

library(patchwork)
a + b + plot_layout(guides = "collect",
                    width = c(3,1),
                    axis_titles = "collect")

combined plot

Created on 2025-04-29 with reprex v2.1.1

Would this approach work for your use-case?

like image 99
jared_mamrot Avatar answered Oct 27 '25 06:10

jared_mamrot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!