How replicate in ggplot2?

Hi community

Im want to replicate this plot but I don`t know how add an extra y.axis in this form.

Some guide?

data_example <- structure(list(Date = structure(c(19297, 19298, 19299, 19300, 
                                                  19301, 19302, 19303, 19304, 19305, 19306, 19307, 19308, 19309, 
                                                  19310, 19311, 19312, 19313, 19314, 19315, 19316, 19317, 19318, 
                                                  19319, 19320, 19321, 19322, 19323, 19324, 19325, 19326), class = "Date"), 
                               P = c(2.7, 6.2, 6.2, 6.2, 6.2, 6.2, 6.2, 6.2, 6.2, 6.2, 6.4, 
                                     6.4, 6.4, 6.4, 23.8, 29.7, 30.3, 30.3, 30.4, 31.4, 32.3, 
                                     32.3, 32.3, 32.3, 32.5, 35.2, 35.2, 35.2, 35.2, 35.2), D = c(0, 
                                                                                                  0, 0, 0.0432551304342697, 0.0432551304342697, 0.0432551304342697, 
                                                                                                  0.0432551304342697, 0.0432551304342697, 0.0432551304342697, 
                                                                                                  0.0432551304342697, 0.0432551304342697, 0.0432551304342697, 
                                                                                                  0.0432551304342697, 0.0432551304342697, 0.0432551304342697, 
                                                                                                  0.0432551304342697, 0.0432551304342697, 0.0432551304342697, 
                                                                                                  0.0432551304342697, 0.0432551304342697, 0.0432551304342697, 
                                                                                                  0.0432551304342697, 0.0432551304342697, 0.0432551304342697, 
                                                                                                  0.0432551304342697, 0.0432551304342697, 0.0432551304342697, 
                                                                                                  0.0432551304342697, 0.0432551304342697, 0.0432551304342697
                                     ), dS = c(0, 0.144394894097204, 1.4002001319444, -0.235532354166693, 
                                               -3.2231233003472, -7.0622712586806, -11.0004165711806, -14.7897904704861, 
                                               -18.0646276173611, -20.6178504911458, -23.3009769368056, 
                                               -25.95491294375, -28.8935023295139, -31.9737702399306, -29.238169625, 
                                               -9.9785169409722, -5.70813761631949, -6.1108179027778, -7.06703965624999, 
                                               -8.9572423159722, -10.0701620104167, -11.5994326510417, -14.5294981684028, 
                                               -18.0743015833333, -20.8018105920139, -21.5491860451389, 
                                               -21.5080001232639, -22.7422270104167, -25.1658506142361, 
                                               -27.9260776472222), E = c(0, 3.3556051059028, 2.0997998680556, 
                                                                         3.69227722373242, 6.67986816991293, 10.5190161282463, 14.4571614407463, 
                                                                         18.2465353400518, 21.5213724869268, 24.0745953607115, 26.9577218063713, 
                                                                         29.6116578133157, 32.5502471990796, 35.6305151094963, 50.2949144945657, 
                                                                         36.9352618105379, 33.2648824858852, 33.6675627723435, 34.7237845258157, 
                                                                         37.6139871855379, 39.6269068799824, 41.1561775206074, 44.0862430379685, 
                                                                         47.631046452899, 50.5585554615796, 54.0059309147046, 53.9647449928296, 
                                                                         55.1989718799824, 57.6225954838018, 60.3828225167879)), row.names = c(NA, 
                                                                                                                                               30L), class = "data.frame")

data_example %>%
  ggplot() +
  geom_line(aes(x = Date, y = P, color = "P"), size = 1) +
  geom_line(aes(x = Date, y = D, color = "D"), size = 1) +
  geom_line(aes(x = Date, y = dS, color = "dS"), size = 1) +
  geom_line(aes(x = Date, y = E, color = "E"), size = 1) +
  scale_color_manual(values = c("P" = "red", 
                                "D" = "green",
                                "dS" = "pink",
                                "E"='orange')) +
  labs(color = "Variable") +  
  theme(legend.position = 'top')

My plot look like these:

Im find something like that:

You can create a secondary axis with:

... +
scale_y_continuous(sec.axis = sec_axis(trans = ~ . - 40,
                                       name = expression(Delta * P),
                                       breaks = c(-25, 0, 25))) 

with this "breaks" argument to only have tick marks at -25 and 25. In addition you can remove the line with

theme(axis.line.y.right = element_blank())

But I don't know how to have a line between the ticks and not outside.

Technically, it is possible to add the text at the end of lines (Lys1, ...), you need to code labels for each series.

So you can try:

library(tidyverse)

data_example <- ...

data_example %>%
  pivot_longer(-Date) %>%
  mutate(series_name = if_else(Date == max(Date),
                               name, NA_character_)) |>
  mutate(series_name = if_else(series_name %in% c("E", "P"),
                               NA_character_,
                               series_name)) |>
  ggplot(aes(x = Date, y = value, color = name)) +
  geom_line(, size = 1) +
  geom_label(aes(label = series_name), show.legend = FALSE) +
  scale_color_manual(values = c("P" = "red", 
                                "D" = "green",
                                "dS" = "pink",
                                "E"='orange')) +
  labs(color = "Variable") +  
  theme_classic() +
  scale_y_continuous(sec.axis = sec_axis(trans = ~ . - 40,
                                         name = expression(Delta * P),
                                         breaks = c(-25, 0, 25))) +
  theme(legend.position = 'top',
        axis.line.y.right = element_blank(),
        axis.title.y.right = element_text(hjust = 0.25))
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: Removed 118 rows containing missing values (`geom_label()`).

Created on 2023-10-31 with reprex v2.0.2

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.