0

I plotted several smooth lines by using different columns in my dataframe. I put those lines into one figure. However, I don't know how to name those lines. Since I do not have "groups" in my dataframe, the legend does not fix the problem.

Sorry that I did not figure out how to upload my data. But I did upload the figure I plotted.

# dfplot is my dataframe
 head(dfplot)
 fall_t  falltsq   winter_t wintertsq spring_t springtsq fall_p  fallpsq winter_p winterpsq spring_p springpsq  ffall_t fwinter_t
1 15.08704 227.6187  1.9648148 3.8604973 14.15000  200.2225   6.12  37.4544     2.83    8.0089    10.27  105.4729 3.303902  3.365150
2 14.67407 215.3284 -0.9666667 0.9344444 13.15000  172.9225  13.89 192.9321     3.21   10.3041    16.02  256.6404 3.043521  3.331537
3 14.13519 199.8035  2.2333333 4.9877778 10.95926  120.1054   7.39  54.6121     6.42   41.2164    17.20  295.8400 3.208130  3.164450
4 15.32963 234.9975 -1.5629630 2.4428532 11.02593  121.5710  11.21 125.6641     4.46   19.8916    13.98  195.4404 2.972689  3.342540
5 14.12222 199.4372 -1.4611111 2.1348457 14.49630  210.1426  10.58 111.9364    11.71  137.1241    12.89  166.1521 3.382247  3.654554
6 13.25926 175.8080  1.0388889 1.0792901 14.82963  219.9179  14.56 211.9936     4.14   17.1396     8.84   78.1456 3.327567  3.323556

  fspring_t
1  3.253946
2  3.087533
3  3.485115
4  3.331752
5  3.213873
6  3.033545

p <- ggplot(data = dfplot) +
  geom_smooth(mapping = aes(x = fall_t, y = ffall_t), color = "red", se = F) +
  geom_smooth(mapping = aes(x = winter_t, y = fwinter_t), color = "blue", se = F) +
  geom_smooth(mapping = aes(x = spring_t, y = fspring_t), color = "green", se = F) 

p + xlab("Temperature") + ylab("log yields") + theme(legend.position="right")

enter image description here

neilfws
  • 32,751
  • 5
  • 50
  • 63
Yabin Da
  • 553
  • 5
  • 11
  • I wrote a post about options when in this situations [here](https://aosmith.rbind.io/2018/07/19/legends-constants-for-aesthetics-in-ggplot2/), since it's something I've seen come up a lot. You might find it helpful. Or see [this answer](https://stackoverflow.com/a/17149021/2461552) on SO – aosmith Jun 04 '19 at 22:32
  • 1
    The standard answer is that your data should be reshaped so that there is a group column with values fall, winter and spring, and then the legend will be generated automatically using `group`. – joran Jun 04 '19 at 22:37

1 Answers1

0

As noted in the comments, ggplot2 works best when data are in "long" format, rather than "wide".

We can use tidyr::gather to transform your example data:

library(tidyverse) # for dplyr, tidyr, ggplot2

dfplot_long <- dfplot %>% 
  select(spring_t, fall_t, winter_t, fspring_t, ffall_t, fwinter_t) %>% 
  gather(Var, Val, 1:3) %>% 
  gather(Var2, Val2, 1:3) %>% 
  filter(Var == gsub("^f", "", Var2)) %>% 
  mutate(season = gsub("_t", "", Var))

dfplot

        Var        Val      Var2     Val2 season
1  spring_t 14.1500000 fspring_t 3.253946 spring
2  spring_t 13.1500000 fspring_t 3.087533 spring
3  spring_t 10.9592600 fspring_t 3.485115 spring
4  spring_t 11.0259300 fspring_t 3.331752 spring
5  spring_t 14.4963000 fspring_t 3.213873 spring
6  spring_t 14.8296300 fspring_t 3.033545 spring
7    fall_t 15.0870400   ffall_t 3.303902   fall
8    fall_t 14.6740700   ffall_t 3.043521   fall
9    fall_t 14.1351900   ffall_t 3.208130   fall
10   fall_t 15.3296300   ffall_t 2.972689   fall
11   fall_t 14.1222200   ffall_t 3.382247   fall
12   fall_t 13.2592600   ffall_t 3.327567   fall
13 winter_t  1.9648148 fwinter_t 3.365150 winter
14 winter_t -0.9666667 fwinter_t 3.331537 winter
15 winter_t  2.2333333 fwinter_t 3.164450 winter
16 winter_t -1.5629630 fwinter_t 3.342540 winter
17 winter_t -1.4611111 fwinter_t 3.654554 winter
18 winter_t  1.0388889 fwinter_t 3.323556 winter

And then:

dfplot_long %>% 
  ggplot(aes(Val, Val2)) + 
  geom_smooth(aes(color = season), se = FALSE)

Result:

enter image description here

neilfws
  • 32,751
  • 5
  • 50
  • 63