Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adjust datetime x-axis to shown year as a sec axis below de main axis

I have the following data example:

structure(list(cycle_rounded = structure(c(1604188800, 1606780800, 
1609459200, 1612137600, 1614556800, 1617235200, 1619827200, 1622505600, 
1625097600, 1627776000, 1630454400, 1633046400, 1635724800, 1638316800, 
1640995200, 1643673600, 1646092800, 1648771200, 1651363200, 1654041600
), tzone = "UTC", class = c("POSIXct", "POSIXt")), media = c(27.0163533756641, 
27.3523628886565, 27.6874774847651, 27.9632663969036, 28.2324797447653, 
28.5946591329209, 28.0774171223198, 27.0218334461407, 26.3074215031224, 
25.762714516129, 25.9331010513628, 26.1019612833841, 27.4678015082705, 
27.8551394156665, 27.61177313685, 28.5468447629364, 28.6978991786281, 
29.0753629090163, NaN, NaN), desvio = c(0.349566729323312, 0.354049553514593, 
0.316576405105695, 0.28004282459359, 0.339372318521672, 0.287591989484519, 
0.312261182269958, 0.246786673478273, 0.358857114435788, 0.119453586990502, 
0.256559466294029, 0.300030737021837, 0.348529074834953, 0.383358876272664, 
0.465191392255828, 0.596852747140639, 0.324808751418437, 0.222745848411023, 
NA, NA), year = c(2020, 2020, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2022, 2022, 2022, 2022, 2022, 
2022)), row.names = c(NA, -20L), class = "data.frame")

And plot at this way:

ggplot(df, aes(x = cycle_rounded, y = media)) +
  geom_ribbon(aes(y = media, ymin = media - desvio, ymax = media + desvio), 
              alpha = .2, fill = "red") +
  geom_line(col = "grey10", linewidth = .5) +
  geom_point(col = "grey10", size = 1) +
  scale_x_datetime(date_labels = "%m", breaks = "1 month",
                   sec.axis = sec_axis(~., sec_axis(~ ., labels = year))) +
  labs(x = "data", y = "temperatura") +
  theme_classic() +
  theme(axis.text.x = element_text(size = 12, angle = 45, 
                                   vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text(size = 12, color = "black"),
        axis.title.x = element_text(size = 14, color = "black"),
        axis.title.y = element_text(size = 14, color = "black"))

however the sec_axis won’t works as I expected.
I would like to shown years below the months as one value of year for all correspondent months.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any helps are appreciated

Thanks,
Wilson

>Solution :

  1. sec_axis(~., sec_axis(~ ., labels = year)) does not make sense as a nested double-call.
  2. Further, labels=year is either a parsing error ('year' not found) or it uses year found in the current or parent environments (whether values or a function).
  3. sec_axis is going to put another axis on the opposite side, in this case the top, not as a second row.

I think we can do a single axis and put the year after a newline.

ggplot(df, aes(x = cycle_rounded, y = media)) +
  geom_ribbon(aes(y = media, ymin = media - desvio, ymax = media + desvio), 
              alpha = .2, fill = "red") +
  geom_line(col = "grey10", linewidth = .5) +
  geom_point(col = "grey10", size = 1) +
  scale_x_datetime(labels = ~ { mon <- format(., format="%m"); if_else(!is.na(mon) & mon == "01", paste0(mon, format(., format = "\n%Y")), mon) },
                   breaks = "1 month") +
  labs(x = "data", y = "temperatura") +
  theme_classic() +
  theme(axis.text.x = element_text(size = 12, angle = 45, 
                                   vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text(size = 12, color = "black"),
        axis.title.x = element_text(size = 14, color = "black"),
        axis.title.y = element_text(size = 14, color = "black"))

ggplot with years under some months

You can easily clean up that code a little by assigning the {...} block to a function.

I assumed putting the year once per 12 months, over to you do adjust this for your expectations.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading