[R] Newbie: Controlling legends in graphs

Kevin Zembower kev|n @end|ng |rom zembower@org
Fri May 12 15:24:19 CEST 2023


Hello, I'm trying to create a line graph with a legend, but have no 
success controlling the legend. Since nothing I've tried seems to work, 
I must be doing something systematically wrong. Can anyone point this 
out to me?

Here's my data:
 > weights
# A tibble: 1,246 × 3
    Date           J     K
    <date>     <dbl> <dbl>
  1 2000-02-13   133  188
  2 2000-02-20   134  185
  3 2000-02-27   135  187
  4 2000-03-05   135  185
  5 2000-03-12    NA  184
  6 2000-03-19    NA  184.
  7 2000-03-26   136  184.
  8 2000-04-02   134  185
  9 2000-04-09   133  186
10 2000-04-16    NA  186
# ℹ 1,236 more rows
# ℹ Use `print(n = ...)` to see more rows
 >

Here's my attempts. You can see some of the things I've tried in the 
commented out sections:
weights %>%
     group_by(year(Date)) %>%
     summarize(
         m_K = mean(K, na.rm = TRUE),
         m_J = mean(J, na.rm = TRUE),
         ) %>%
     ggplot(aes(x = `year(Date)`)) +
     geom_point(aes(y = m_K, color = "red")) +
     geom_smooth(aes(y = m_K, color = "red")) +
     geom_point(aes(y = m_J, color = "blue")) +
     geom_smooth(aes(y = m_J, color = "blue")) +
     guides(size = "legend",
            shape = "legend")
     ## scale_shape_discrete(name="Person",
     ##                      breaks=c("m_K", "m_J"),
     ##                      labels=c("K", "J"))
     ## theme(legend.title=element_blank())

When this runs, the blue line for "K" is above the red line for "J", as 
I expect, but in the legend, the red is shown first, and labeled "blue."

I'd like to be able to create a legend where the first entry shows a 
blue line and is labeled "K" and the second is red and labeled "J".

On a different but related topic, I'd welcome any advice or suggestions 
on my methodology in this example. Is this the correct way to summarize 
with a mean? Do I need the two sets of geom_point and geom_line clauses 
to create this graph, or is there a better way?

Thanks for all your advice and guidance.

-Kevin




More information about the R-help mailing list