Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the split function for timeVariation and problem with multiple legends/Keys being created. #371

Open
mjb302 opened this issue Nov 3, 2023 · 2 comments
Labels
question Questions about function use or interpretation

Comments

@mjb302
Copy link

mjb302 commented Nov 3, 2023

Question

Hi David,

I am using the split function to create a 3 column 4 row plot of multiple time variation subplots (a-d) i.e.

print(a, split = c(1, 1, 3, 4), subset = "hour")
print(a, split = c(2, 1, 3, 4), subset = "month", newpage = FALSE)
print(a, split = c(3, 1, 3, 4), subset = "day", newpage = FALSE)
print(b, split = c(1, 2, 3, 4), subset = "hour", newpage = FALSE)
print(b, split = c(2, 2, 3, 4), subset = "month", newpage = FALSE)
print(b, split = c(3, 2, 3, 4), subset = "day", newpage = FALSE)
print(c, split = c(1, 3, 3, 4), subset = "hour", newpage = FALSE)
print(c, split = c(2, 3, 3, 4), subset = "month", newpage = FALSE)
print(c, split = c(3, 3, 3, 4), subset = "day", newpage = FALSE)
print(d, split = c(1, 4, 3, 4), subset = "hour", newpage = FALSE)
print(d, split = c(2, 4, 3, 4), subset = "month", newpage = FALSE)
print(d, split = c(3, 4, 3, 4), subset = "day", newpage = FALSE)

However the legends are printing over each other - This does not appear to be helped by setting the key value to True or False. I have attached an image file to show what is being produced by the code above.

Any suggestions?

Thanks in advance

Matt
Timevariation split plot

@mjb302 mjb302 added the question Questions about function use or interpretation label Nov 3, 2023
@jack-davison
Copy link
Collaborator

jack-davison commented Nov 5, 2023

Hi @mjb302,

@davidcarslaw may have an answer from within {openair} itself, but if you're comfortable with {ggplot2} you could give this a go w/ the in-development {ggopenair}. Under that framework the time variation plots are {patchwork} ensembles so you can pull the individual panels out and treat them however you'd like:

library(patchwork)

# import data
dat <- openair::importAURN(c("my1", "kc1"))

# run timevar to get lower panels without legends
timevar <-
  ggopenair::trend_variation(
    dat, group = "site"
  )

# index to get panels of interest
days <- timevar[[2]][[2]]
month <- timevar[[2]][[3]]

# run again to get the hour panel with a legend
hour <-
  ggopenair::trend_variation(
    dat, group = "site",
    return = "hour"
  ) +
  ggplot2::labs(color = NULL, fill = NULL)

# patchwork it together
(
  (
    (days | month | hour) /
      (days | month | hour) /
      (days | month | hour)
  ) &
    ggplot2::theme_bw() &
    ggplot2::theme(legend.position = "top")
) +
  patchwork::plot_layout(guides = "collect")

Created on 2023-11-05 with reprex v2.0.2

@mjb302
Copy link
Author

mjb302 commented Nov 17, 2023

Many thanks Jack,

That is really helpful and thank you for taking the time to write such a comprehensive reply. I thought i'd share with you the output as you facilitated it.

image

Presumably now it is compatible with ggplot2 you can change the line types to dashes as well as the line colour?

I figured I could do this in openair using cols and lty as per the below - Is hat possible using this combination of ggopenair?

timeVariation(
iaqMatched,
pollutant = "pm2_5",
cols = c("grey","black","lightblue","lightblue","darkred", "red", "purple","purple"),
lty = c(5,1,3,4,1,2,1,1),
group= "HEPAperiodward",
normalise = FALSE,
xlab = c("hour", "hour", "month", "weekday"),
ylab = "pm2.5 (ug/m3)",
plot = TRUE,
)

Best wishes

Matt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions about function use or interpretation
Projects
None yet
Development

No branches or pull requests

2 participants