Hi,
I want to plot a dual Y axis plot in R using ggplot2. I am unable to make it.
code
> # z-score and dtd
>
> ggplot(data_plot, aes(x = year, y = avg_DtD)) +
> geom_line()
>
> # Add the second y-axis
> ggplot(data_plot, aes(x = year, y = avg_DtD)) +
> geom_line() +
> sec_axis(~avg_z_sco, name = "Avg z-score",
> breaks = c(0, 1, 2, 2.5))
>
> # Add a title and labels to the plot
> ggplot(data_plot, aes(x = year, y = avg_DtD)) +
> geom_line() +
> sec_axis(~avg_z_sco, name = "Avg z-score",
> ylim = c(0, 2.5)) +
> labs(title = "Dual Y-Axis Chart",
> x = "Year",
> y = "Avg DtD")
data
|year|avg_DtD|avg_z_score|
|---|---|---|
|1998|5.168958819|2.622686043|
|1999|4.61623166|1.714489031|
|2000|4.473965641|1.383415663|
|2001|2.966609387|1.153488768|
|2002|2.077829803|1.539516503|
|2003|1.587460099|1.766625354|
|2004|2.363948772|2.027161624|
|2005|2.622798157|1.347363288|
|2006|2.885535875|1.631937691|
|2007|2.976895885|1.92667189|
|2008|1.732436579|2.119474966|
|2009|2.840667346|2.094776486|
|2010|3.025728663|1.696732448|
|2011|3.381808742|2.188792639|
|2012|3.116533142|2.213063948|
|2013|3.462095588|1.922371661|
|2014|3.010478683|1.655947946|
|2015|2.976423025|1.61989776|
|2016|2.314551387|1.26567022|
|2017|2.736277955|1.139380448|
|2018|2.782182781|0.672269729|
|2019|2.394159901|0.821119027|
|2020|2.011479927|0.997928868|
|2021|2.196323599|1.208286139|
|2022|2.655758237|1.265071114|
Thanks in advance.