Hi,
I am trying to plot a multi y-axis plot. I am not able to get legend for the same. The legend should be according to line type and placed at bottom. I am sharing the data and codes I tried. Additionally I need a similarly plot for Avg_z_score and CR5 vs Year.
Thank you in advance.
code
# z-score and dtd
transform_sec_axis <- function(x) {
return(x)
}
ggplot(data_plot, aes(year)) +
geom_line(aes(y = avg_z_score, linetype = "Avg. z-score"), lty = 1, size = 1.1,show.legend = TRUE) +
geom_line(aes(y = avg_DtD, linetype = "Avg. DtD"), lty = 4, size = 1.1,show.legend = TRUE) +
scale_y_continuous(
name = "Avg. z-score",
breaks = seq(0, 6, by = 1),
limits = c(0, 6),
sec.axis = sec_axis(
name = "Avg. DtD",
breaks = seq(0, 6, by = 1),
trans = transform_sec_axis
)
) +
scale_linetype_manual(name= NULL,values = c("Avg. z-score" = "1", "Avg. DtD" = "4")) +
theme(legend.position = "bottom",
axis.title.x = element_blank(),
legend.title = element_text(),
legend.key.width = unit(3, "cm"),
axis.title.y = element_text(colour = "black", face = "bold"), # Darken y-axis label
axis.text.x = element_text(colour = "black", face = "bold"), # Darken x-axis tick labels
axis.text.y = element_text(colour = "black", face = "bold"), # Darken y-axis tick labels
legend.text = element_text(colour = "black", face = "bold")) # Darken legend text
Data
year CR5 avg_DtD avg_z_score
1 1998 0.4428660 5.168959 2.6226860
2 1999 0.4421126 4.616232 1.7144890
3 2000 0.4282841 4.473966 1.3834157
4 2001 0.4275604 2.966609 1.1534888
5 2002 0.4207379 2.077830 1.5395165
6 2003 0.4149098 1.587460 1.7666254
7 2004 0.4057806 2.363949 2.0271616
8 2005 0.4091860 2.622798 1.3473633
9 2006 0.4098896 2.885536 1.6319377
10 2007 0.4008190 2.976896 1.9266719
11 2008 0.3914018 1.732437 2.1194750
12 2009 0.3907538 2.840667 2.0947765
13 2010 0.3789176 3.025729 1.6967324
14 2011 0.3784298 3.381809 2.1887926
15 2012 0.3747106 3.116533 2.2130639
16 2013 0.3743605 3.462096 1.9223717
17 2014 0.3805648 3.010479 1.6559479
18 2015 0.3866679 2.976423 1.6198978
19 2016 0.3922078 2.314551 1.2656702
20 2017 0.4242170 2.736278 1.1393804
21 2018 0.4514388 2.782183 0.6722697
22 2019 0.4588402 2.394160 0.8211190
23 2020 0.5184670 2.011480 0.9979289
24 2021 0.5124125 2.196324 1.2082861
25 2022 0.5168476 2.655758 1.2650711