0

I have a series of plots of types of nitrogen. Two are detected in much lower levels in water than another. There are dynamics between the red and the blue line that I'd like to be able to see. But the scale is stretched too high because of the green line. See below:

enter image description here

Is there a way to change the intervals of the y axis to show the patterns going on between 0 and 0.5?

A bit like this question: Y-break with scale change in R

Current Code

sampleSites <- list("BVR1", "BVR2", "BVR3", "BVR4","BVR5", "BVR6")

samplePlotList <- list()


for (site in sampleSites){
  dat <- subset(nitrogen, ID==site)
  samplePlotList[[site]] <-
    dat %>%
    ggplot(aes_string(x="Month", y="value", group="condition")) +
    geom_line(size=0.5, aes(colour=condition)) +
    theme_classic() +
    theme(legend.position='none') +
    theme(text=element_text(size=10, family='Calibri')) +
    theme(axis.text=element_text(size=10, family='Calibri')) +
    labs(x= NULL) +
    ylim(0, 9) +
    theme(plot.margin = margin(0.75,0,0.5,0, "cm"))
}

Data Frame Head

structure(list(ID = structure(1:6, levels = c("BVR1", "BVR2", 
"BVR3", "BVR4", "BVR5", "BVR6"), class = "factor"), Survey = c(1L, 
1L, 1L, 1L, 1L, 1L), Month = structure(c(1L, 1L, 1L, 1L, 1L, 
1L), levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
"Aug", "Sep", "Oct", "Nov", "Dec"), class = "factor"), Date = c("24/01/2022", 
"24/01/2022", "24/01/2022", "24/01/2022", "24/01/2022", "24/01/2022"
), Total.Nitrogen = c(4.987497648, 4.901992077, 3.837696783, 
4.389720566, 3.853376279, 1.696494226), DO = c(9, 9.4, 8.3, 8.5, 
8.8, 8.7), condition = c("Ammonia", "Ammonia", "Ammonia", "Ammonia", 
"Ammonia", "Ammonia"), value = c(0.063, 0.066888889, 0.065333333, 
0.035, 0.052888889, 0.035)), row.names = c(NA, 6L), class = "data.frame")
Melanie Baker
  • 449
  • 1
  • 13
  • If you want to keep all 3 lines, you could put a secondary axis for the green line and reducing the ylim for the primary axis to your desired level – Alik Nov 15 '22 at 11:22

1 Answers1

0

The most straightforward and clearest way would be to construct two graphs then stack them on top of one another. This gives a bit of control over layout and additionally avoids manipulating the scale of the graph per se.

The patchwork package is useful for stacking graphs together:

library(tidyverse)
library(patchwork)

df <- tibble(condition = factor(rep(c("A", "B", "C"), each = 12)),
             time = factor(rep(month.abb, 3), levels = month.abb),
             value = c(rnorm(12, 40, 1), rnorm(24, 3, 0.5)))
  
g1 <- df |> 
  filter(condition == "A") |> 
  ggplot(aes(time, value, group = condition, colour = condition)) +
  geom_line() +
  scale_colour_discrete("Condition",  drop = FALSE) +
  ylim(35, NA) +
  theme(axis.ticks.x = element_blank(), axis.title.x = element_blank(), axis.text.x = element_blank())

g2 <- df |> 
  filter(condition != "A") |> 
  ggplot(aes(time, value, group = condition, colour = condition)) +
  geom_line() +
  scale_colour_discrete("Condition",  drop = FALSE) +
  ylim(0, 5)

g1/g2 + plot_layout(guides = "collect")

Andy Baxter
  • 5,833
  • 1
  • 8
  • 22