如何在ggplot中命名不同的geom_smooth线?

时间:2019-06-04 22:16:20

标签: r ggplot2

我在数据框中使用不同的列绘制了几条平滑线。我把这些线变成一个数字。但是,我不知道如何命名这些行。由于我的数据框中没有“组”,因此图例无法解决问题。

很抱歉,我没有弄清楚如何上传数据。但是我确实上传了我绘制的图。

# 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

1 个答案:

答案 0 :(得分:0)

如注释中所述,ggplot2在数据为“长”格式而不是“宽”格式时效果最佳。

我们可以使用tidyr::gather来转换您的示例数据:

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

然后:

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

结果:

enter image description here