Resize the scale on the dual y-axis and on an area chart

40 Views Asked by At

I want to scale the geom_line chart so that the area chart looks like this image (example). In my example I was able to scale both Y-axes but geom_line does not obey the second Y-axis. enter image description here

This is my code

library(ggplot2)

df <- read.csv("route/file.csv")
gas <- as.data.frame(df[, 1:3])

ggplot(gas) +
  geom_area(aes(x = año, y = qg, fill = " Qg [MMMpc]"),
            colour = "#ffed63", alpha = 0.9) +
  geom_line(aes(x = año, y = np, colour = "Gp [MMMpc]"),
            linewidth=1, linetype = "dashed")+
  scale_colour_manual(values = "#C5B21D")+
  scale_fill_manual(values = "#ffed63")+
  guides(colour = guide_legend(override.aes = list(fill = "black"))) +
  scale_x_continuous(breaks = gas$año) +
  scale_y_continuous(breaks = seq(0, 60, 10),
                     limits = c(0, 180),
                     sec.axis = sec_axis(trans = ~ .*3,
                                         breaks = seq(0, 180, 30),
                                         labels = seq(0, 180, 30),
                                         name = "Total acumulado [MMMpc]")))

and the data is

    año    qg     np
1  2019  0.33   0.12
2  2020 13.05   4.90
3  2021 14.88  10.33
4  2022 13.67  15.32
5  2023 12.59  19.91
6  2024 12.23  24.39
7  2025 11.33  28.52
8  2026 16.32  34.48
9  2027 49.21  52.44
10 2028 48.21  70.09
11 2029 47.06  87.27
12 2030 42.40 102.74
13 2031 31.85 114.37
14 2032 24.56 123.36
15 2033 19.61 130.51
16 2034 16.06 136.38
17 2035 13.42 141.28
18 2036 11.43 145.46
19 2037  9.80 149.04
20 2038  8.49 152.13
21 2039  7.35 154.82
22 2040  5.97 157.00
23 2041  4.21 158.54
24 2042  3.70 159.89
0

There are 0 best solutions below