Put names to layers of ggplot

Hello, I want to put a name for each layer and read it in the legend in a ggplot graph.
It's the code:

ggplot(filter(data,Year %in% c(y0:y1)))+aes(x = Year,group = code) +
  facet_wrap(.~code,scales = "free")+scale_x_continuous(breaks = c(seq(y0,y1,5)),minor_breaks = c(y0:y1))+
  geom_vline(xintercept = c(y0,y1),color="grey60")+theme_minimal()+
  geom_line(aes(y =gdp),na.rm = TRUE) +
geom_line(aes(y = fiscal_deficit), linetype = 2) 

I want to name the GDP series and name the fiscal deficit series and read it in the legend.

The data:

structure(list(code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("ARG", "BOL", "BRA", 
"CHL", "COL", "CRI", "DOM", "ECU", "GTM", "HND", "MEX", "NIC", 
"PAN", "PER", "PRY", "SLV", "URY", "VEN"), class = "factor"), 
    Year = c(2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
    2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 
    2018, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
    2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 
    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
    2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2000, 
    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
    2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2000, 2001, 
    2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 
    2012, 2013, 2014, 2015, 2016, 2017, 2018), gdp= c(-1.87491241541325, 
    -5.44709154527357, -11.8547768361233, 7.6765455385672, 7.88493756497286, 
    7.72947520461582, 6.95554738176538, 7.92423813871991, 3.02947731150856, 
    -6.85422733296183, 9.30012832232192, 4.78868313859434, -2.14528444981811, 
    1.26568517537076, -3.57858050983292, 1.62966427946265, -3.11006389821465, 
    1.60928234377617, -3.46737624314432, 0.544926184712779, -0.235706734357976, 
    0.579771054946548, 0.831349050253706, 2.2984817380683, 2.57481330640607, 
    2.97734206021694, 2.78132356894432, 4.36853739987981, 1.64922554346305, 
    2.42927786018541, 3.51097503109318, 3.45369767643476, 5.12757347801841, 
    3.84375722818145, 3.28271871061374, 2.73204225738901, 2.69606298190565, 
    2.75215380976242, 2.91182644263147, 0.0133697384164577, 1.70947438880017, 
    -0.122936492113425, 4.49707707823583, 2.02630740411568, 2.8347540947357, 
    4.97470467428765, 4.05440956846654, -1.08194389571234, 6.52437280430098, 
    3.02640687298958, 1.01450977525141, 2.10891183467072, -0.352479217688355, 
    -4.35148398862317, -4.0694071643008, 0.508336053889494, 0.526155826049262, 
    4.09428735354267, 2.14618943830904, 2.00061767493953, 3.00583035674246, 
    6.10283936667567, 4.64670086980279, 5.20045134165797, 3.79033938890592, 
    2.42619717171779, -2.601446897912, 4.75022381395627, 5.05778792023305, 
    4.30921750358573, 3.03152589042497, 0.692556386631992, 1.10594434535712, 
    0.372104022166411, -0.243323996034391, 2.51338504277558, 
    1.28736794818924, 0.0967597236302424, 0.950715907776441, 
    2.39393954882721, 3.85079290108337, 3.42334654264872, 5.35039250871598, 
    5.4286154432716, 2.07578650993585, 0.0207200267183794, 3.40253084514683, 
    5.91643012933557, 2.97954787156493, 4.18339295193478, 3.44903400869727, 
    1.76114004205137, 0.708331383936113, -0.153066085789078, 
    0.971629907629179), fiscal_deficit= c(-3.526, -5.617, -1.979, 
    1.548, 3.966, 3.34, 1.65, 0.757000000000001, 0.352, -1.83, 
    -1.393, -2.746, -3.018, -3.254, -4.252, -6, -6.655, -6.703, 
    -5.224, -3.728, -6.82, -8.788, -7.876, -5.544, -2.245, 4.47, 
    1.74, 3.569, 0.0140000000000029, 1.666, 0.830999999999996, 
    1.75700000000001, 0.656000000000006, -3.364, -6.904, -7.243, 
    -7.824, -8.143, -3.32, -3.171, -4.424, -5.182, -2.88, -3.543, 
    -3.574, -2.739, -1.534, -3.19, -3.825, -2.467, -2.519, -2.956, 
    -6.021, -10.254, -8.997, -7.895, -7.231, -0.697000000000003, 
    -0.498999999999999, -1.18, -0.420000000000002, 2.047, 4.543, 
    7.46, 7.909, 3.935, -4.241, -0.361000000000001, 1.422, 0.680999999999997, 
    -0.469999999999999, -1.491, -2.076, -2.65, -2.615, -1.465, 
    -2.964, -2.927, -3.467, -2.715, -1.276, -0.0130000000000017, 
    -1, -0.854000000000003, 0.0420000000000016, -2.672, -3.308, 
    -1.996, 0.106999999999999, -1.076, -1.879, -3.519, -2.425, 
    -2.586, -2.698)), row.names = c(NA, -95L), class = c("data.table", 
"data.frame"))

I want to give a name to the GDP series (solid line) and a name to the fiscal deficit series (broken line) and read in the legend.
image

We probably nee some sample data at least

1 Like

Here is a simplified version of your graphs. The key is to reshape the data into a long format with a column designating whether the value is a gdp or a fiscal deficit. I used the old gather function because the computer I am on has an old version of tidyr. The latest function for reshaping the data in this way is pivot_longer().

library(ggplot2)
library(tidyr)
DF <- structure(list(code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
                                        2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
                                        3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                        3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                        4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
                                        5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("ARG", "BOL", "BRA", 
                                                                                    "CHL", "COL", "CRI", "DOM", "ECU", "GTM", "HND", "MEX", "NIC", 
                                                                                    "PAN", "PER", "PRY", "SLV", "URY", "VEN"), class = "factor"), 
                     Year = c(2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
                              2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 
                              2018, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
                              2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 
                              2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
                              2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2000, 
                              2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
                              2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2000, 2001, 
                              2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 
                              2012, 2013, 2014, 2015, 2016, 2017, 2018), 
                     gdp= c(-1.87491241541325, -5.44709154527357, -11.8547768361233, 7.6765455385672, 
                            7.88493756497286,7.72947520461582, 6.95554738176538, 7.92423813871991, 
                            3.02947731150856, -6.85422733296183, 9.30012832232192, 4.78868313859434, 
                            -2.14528444981811, 1.26568517537076, -3.57858050983292, 1.62966427946265, -3.11006389821465, 
                            1.60928234377617, -3.46737624314432, 0.544926184712779, -0.235706734357976, 
                            0.579771054946548, 0.831349050253706, 2.2984817380683, 2.57481330640607, 
                            2.97734206021694, 2.78132356894432, 4.36853739987981, 1.64922554346305, 
                            2.42927786018541, 3.51097503109318, 3.45369767643476, 5.12757347801841, 
                            3.84375722818145, 3.28271871061374, 2.73204225738901, 2.69606298190565, 
                            2.75215380976242, 2.91182644263147, 0.0133697384164577, 1.70947438880017, 
                            -0.122936492113425, 4.49707707823583, 2.02630740411568, 2.8347540947357, 
                            4.97470467428765, 4.05440956846654, -1.08194389571234, 6.52437280430098, 
                            3.02640687298958, 1.01450977525141, 2.10891183467072, -0.352479217688355, 
                            -4.35148398862317, -4.0694071643008, 0.508336053889494, 0.526155826049262, 
                            4.09428735354267, 2.14618943830904, 2.00061767493953, 3.00583035674246, 
                            6.10283936667567, 4.64670086980279, 5.20045134165797, 3.79033938890592, 
                            2.42619717171779, -2.601446897912, 4.75022381395627, 5.05778792023305, 
                            4.30921750358573, 3.03152589042497, 0.692556386631992, 1.10594434535712, 
                            0.372104022166411, -0.243323996034391, 2.51338504277558, 
                            1.28736794818924, 0.0967597236302424, 0.950715907776441, 
                            2.39393954882721, 3.85079290108337, 3.42334654264872, 5.35039250871598, 
                            5.4286154432716, 2.07578650993585, 0.0207200267183794, 3.40253084514683, 
                            5.91643012933557, 2.97954787156493, 4.18339295193478, 3.44903400869727, 
                            1.76114004205137, 0.708331383936113, -0.153066085789078, 
                            0.971629907629179), 
                     fiscal_deficit= c(-3.526, -5.617, -1.979, 
                                        1.548, 3.966, 3.34, 1.65, 0.757000000000001, 0.352, -1.83, 
                                        -1.393, -2.746, -3.018, -3.254, -4.252, -6, -6.655, -6.703, 
                                        -5.224, -3.728, -6.82, -8.788, -7.876, -5.544, -2.245, 4.47, 
                                        1.74, 3.569, 0.0140000000000029, 1.666, 0.830999999999996, 
                                        1.75700000000001, 0.656000000000006, -3.364, -6.904, -7.243, 
                                        -7.824, -8.143, -3.32, -3.171, -4.424, -5.182, -2.88, -3.543, 
                                        -3.574, -2.739, -1.534, -3.19, -3.825, -2.467, -2.519, -2.956, 
                                        -6.021, -10.254, -8.997, -7.895, -7.231, -0.697000000000003, 
                                        -0.498999999999999, -1.18, -0.420000000000002, 2.047, 4.543, 
                                        7.46, 7.909, 3.935, -4.241, -0.361000000000001, 1.422, 0.680999999999997, 
                                        -0.469999999999999, -1.491, -2.076, -2.65, -2.615, -1.465, 
                                        -2.964, -2.927, -3.467, -2.715, -1.276, -0.0130000000000017, 
                                        -1, -0.854000000000003, 0.0420000000000016, -2.672, -3.308, 
                                        -1.996, 0.106999999999999, -1.076, -1.879, -3.519, -2.425, 
                                        -2.586, -2.698)), row.names = c(NA, -95L), 
                class = c("data.table", "data.frame"))

DFLng <- gather(DF,gdp, fiscal_deficit, key = "Meas", value = "Value")

ggplot(DFLng,aes(x = Year)) +
  facet_wrap(.~code,scales = "free")+
  theme_minimal()+
  geom_line(aes(y = Value, linetype = Meas),na.rm = TRUE) +
  scale_linetype_manual(values = c(gdp = 1, fiscal_deficit = 2))

Created on 2021-01-26 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 7 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.

I have put the data and the result.