Legend help with a ggplot

Hello,

I am trying to create a plot with two lines: one representing the data, and another a LOESS line of the data. I have been able to create this plot, but I am sure there is a cleaner way to do this.

Primary question: I was having problems adding the legend.

What I did:
- I used "group = 1"
- In both the geom_line and geom_smooth, I added "color = "Mean Annual Flow" or "LOESS"

But I thought "color = " codes for the color of the line. Why does this create the legend key name?

Is there a better way to do this?

Thank you.

Craig

Code:

plot3c <- ellaville_yearly %>%
  ggplot(aes(x= year, y = year_avg, group = 1)) +
  geom_line(aes(color = "Mean Annual Flow"), size =1) +      
  geom_smooth(aes(color = "LOESS"), method = "loess", se = F, linetype = "dashed", size = 1, span = 0.4,
              show.legend = TRUE) +
  labs(x=expression(paste("Year")), 
       colour = "Test",                               
       y=expression(paste("Flow ( ",ft^3,s^-1,")"))) +
  
  theme(legend.position = "bottom")
#> Error in ellaville_yearly %>% ggplot(aes(x = year, y = year_avg, group = 1)): could not find function "%>%"
plot3c
#> Error in eval(expr, envir, enclos): object 'plot3c' not found

Created on 2022-05-23 by the reprex package (v2.0.1)

Data:

tibble::tribble(
                  ~year,        ~year_avg,
                   1932, 7984.67391304348,
                   1933, 6842.02739726027,
                   1934, 2201.09589041096,
                   1935, 3535.01369863014,
                   1936, 5053.44262295082,
                   1937, 7557.94520547945,
                   1938, 2445.67123287671,
                   1939, 4354.35616438356,
                   1940, 3260.02732240437,
                   1941,  2414.6301369863,
                   1942, 6919.31506849315,
                   1943, 2564.30136986301,
                   1944, 8430.02732240437,
                   1945, 7805.50684931507,
                   1946, 8418.76712328767,
                   1947, 11690.0547945205,
                   1948, 14529.4808743169,
                   1949, 7174.05479452055,
                   1950, 3040.52054794521,
                   1951, 3737.17808219178,
                   1952, 4722.67759562842,
                   1953, 6311.17808219178,
                   1954, 2994.18904109589,
                   1955, 1383.02739726027,
                   1956, 2537.10928961749,
                   1957, 5104.35616438356,
                   1958, 8195.06849315069,
                   1959, 10391.1780821918,
                   1960, 8230.79234972678,
                   1961, 5720.38356164384,
                   1962, 4000.43835616438,
                   1963, 4076.84931506849,
                   1964, 15573.9344262295,
                   1965, 10968.4383561644,
                   1966, 9773.39726027397,
                   1967, 4781.15068493151,
                   1968, 1672.00546448087,
                   1969, 4780.57534246575,
                   1970, 8447.28767123288,
                   1971,  6456.6301369863,
                   1972,  7294.4262295082,
                   1973, 12112.3835616438,
                   1974,  4608.1095890411,
                   1975, 8386.87671232877,
                   1976, 7336.47540983607,
                   1977,  6425.3698630137,
                   1978, 5799.56164383562,
                   1979, 5688.68493150685,
                   1980, 6209.56284153005,
                   1981, 2424.79452054795,
                   1982, 4774.68493150685,
                   1983,  9491.2602739726,
                   1984, 11578.4426229508,
                   1985, 3679.15068493151,
                   1986, 7152.57534246575,
                   1987, 8382.27397260274,
                   1988, 4655.92896174863,
                   1989, 2470.16438356164,
                   1990, 3436.14794520548,
                   1991, 13247.4794520548,
                   1992, 6384.48087431694,
                   1993, 6175.72602739726,
                   1994, 9673.45205479452,
                   1995, 5414.57534246575,
                   1996, 3843.25136612022,
                   1997, 6890.35616438356,
                   1998, 10770.1369863014,
                   1999, 1862.98630136986,
                   2000, 1817.45628415301,
                   2001, 3211.11780821918,
                   2002, 1933.83835616438,
                   2003, 8260.02739726027,
                   2004, 7044.20765027322,
                   2005, 9233.39726027397,
                   2006, 3181.82465753425,
                   2007, 1610.55342465753,
                   2008, 4539.09836065574,
                   2009, 5779.06849315068,
                   2010, 5074.97808219178,
                   2011, 1263.07945205479,
                   2012,  2925.8306010929,
                   2013, 7449.83561643836,
                   2014,  8116.1095890411,
                   2015, 7178.57142857143
                  )
#> # A tibble: 84 x 2
#>     year year_avg
#>    <dbl>    <dbl>
#>  1  1932    7985.
#>  2  1933    6842.
#>  3  1934    2201.
#>  4  1935    3535.
#>  5  1936    5053.
#>  6  1937    7558.
#>  7  1938    2446.
#>  8  1939    4354.
#>  9  1940    3260.
#> 10  1941    2415.
#> # ... with 74 more rows

Created on 2022-05-23 by the reprex package (v2.0.1)

Try This -

ellaville_yearly %>% 
    ggplot(aes(year, year_avg))+
    geom_line(aes(color = "blue"), size =1.5) + 
    geom_smooth(aes(color = "orange"), method = "loess", se = F, linetype = "dashed", size = 1, span = 0.4, show.legend = TRUE) +
    scale_color_identity(name = "Legend Name",
                         labels = c("Annual Avg", "Loess"),
                         guide = "legend")
2 Likes

@ML_Rookie_2021 --

Thank you so much!

A much cleaner, and understandable code.

Best,

Craig

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.