0

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.

Any helps are appreciated

Thanks, Wilson

Wilson Souza
  • 830
  • 4
  • 12
  • 1
    I think it is not clear what you exactly mean with secondary axis? – TarJae Jun 16 '23 at 18:26
  • 1
    I would like the month on first row of x-axis and year in the row below. I called secondary axis to refer the row of year. – Wilson Souza Jun 16 '23 at 18:33
  • something like this? https://stackoverflow.com/questions/56339970/ggplot2-add-secondary-x-label-year-below-months – AdroMine Jun 16 '23 at 18:37
  • Included ggplot code seems to have some issues and at least form me it fails at `sec.axis`. Perhaps include an image of what you have now and manually annotate it to illustrate what you'd like to achieve? – margusl Jun 16 '23 at 18:48
  • @AdroMine exactly this, but it wont works for my data. The last month of the previous year plot above the first month of next year and the lines not connect. – Wilson Souza Jun 16 '23 at 19:22

1 Answers1

1
  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.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Is possible to add all years that have? For example, 2020 not appear. And adding this second row, is possible format they different, for example, use the angle as 0 and not 45°? Sorry for this aestethics questions. – Wilson Souza Jun 16 '23 at 18:56
  • 1
    It's all in the `labels=` function. Perhaps (untested) `if_else(!is.na(mon) & (mon == "01" | seq_along(mon) == which(!is.na(mon))[1]), paste0(mon, format(., format = "\n%Y")), mon)` or similar will force the first non-`NA` month to add the year to it – r2evans Jun 16 '23 at 20:17