'How to set facet plot y axis limits for each column

I have a sample dataset as below:

Day<-c("1","1","1","2","2","2")
Group<-c("Blue","Red","Green","Blue","Red","Green")
UV<-c("3","4","2","5","4","6")
Rain<-c("10","11","12","15","16","17")

dmean<-data.frame(Day,Group,UV,Rain)

Day<-c("1","1","1","1","1","1","2","2","2","2","2","2")
Group<-c("Blue","Blue","Red","Red","Green","Green","Blue","Blue","Red","Red","Green","Green")
UV<-c("3","3.1","4","4.1","2","2.2","5","5.1","4","4.2","6.1","6.1")
Rain<-c("10","10.1","11","11","12","12.2","15","15.2","16","16.1","17","17.2")

dpoints<-data.frame(Day,Group,UV,Rain)

library(ggplot2)

plot.ts <- function(yvar) {
  ggplot(dmean, aes(x = Day, y = .data[[yvar]], group = Group, colour = Group)) +
    geom_line(size = 1)+
    geom_point(data = dpoints, aes(y = .data[[yvar]]), alpha = .2) +
    facet_wrap(~Group, ncol = 1)
    
}

lapply(names(dpoints)[3:4], plot.ts)

Is it possible to modify the function "plot.ts" so I can set y axis limits for each "yvar" column, in this case "UV" and "Rain"? I have a larger dataset where there are more columns besides "UV" and "Rain" to apply the "plot.ts" function on.

Thank you.



Solution 1:[1]

We can use the {facetscales} package. We need to create a list with each column name and can give each column name its own scale_ function. If you want to pass each iteration its own list, then we have to create a list of lists and use mapply or purrr::map2.

library(ggplot2)
library(facetscales)

plot.ts <- function(yvar, ylimits = NULL) {
  
  p <- ggplot(dmean, aes(x = Day, y = .data[[yvar]], group = Group, colour = Group)) +
    geom_line(size = 1)+
    geom_point(data = dpoints, aes(y = .data[[yvar]]), alpha = .2)
  
  if(!is.null(ylimits)) {
    p +  facet_grid_sc(rows = vars(Group), scales = list(y = ylimits))
  } else
    p + facet_grid(rows = vars(Group))
  
}

scales_y <- list(
  Blue = scale_y_continuous(limits = c(0,50)),
  Green = scale_y_continuous(limits = c(12,14)),
  Red = scale_y_continuous(limits = c(10,25))
)

lapply(names(dpoints)[3:4], plot.ts, ylimits = scales_y)
#> [[1]]

#> 
#> [[2]]

Created on 2022-05-02 by the reprex package (v0.3.0)

Below the slightly altered data:

Day<-c("1","1","1","2","2","2")
Group<-c("Blue","Red","Green","Blue","Red","Green")
UV<-c("3","4","2","5","4","6")
Rain<-c("10","11","12","15","16","17")


dmean<-data.frame(Day,Group,UV,Rain)
dmean$UV <- as.numeric(dmean$UV)
dmean$Rain <- as.numeric(dmean$Rain)

Day<-c("1","1","1","1","1","1","2","2","2","2","2","2")
Group<-c("Blue","Blue","Red","Red","Green","Green","Blue","Blue","Red","Red","Green","Green")
UV<-c("3","3.1","4","4.1","2","2.2","5","5.1","4","4.2","6.1","6.1")
Rain<-c("10","10.1","11","11","12","12.2","15","15.2","16","16.1","17","17.2")

dpoints<-data.frame(Day,Group,UV,Rain)

dpoints$UV <- as.numeric(dmean$UV)
dpoints$Rain <- as.numeric(dmean$Rain)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TimTeaFan