将图例添加到多个回归图

时间:2018-05-10 10:03:45

标签: r ggplot2 regression legend

我试图用4种不同颜色的相应点绘制4个回归图,所有回归图都具有相同的预测值。我需要一个为4种颜色分配4个标签的图例。仅仅是一个有2列的简单表。但是,在尝试使用scale_linetype_manualor scale_fill_manual设置手册时,不会显示任何图例。

Forest_EPs_pure <- data.frame("Sand"=c(23,41,32,34,38,49,32,18,91,18,117,61,68,43,46,59,74,88,58,92,882,941,870,926,861,848,810,964,874,860,755,942),
                              "B2_fs"=c(61,68,80,20,43,72,93,60,75,75,97,83,74,51,87.5,74,97,74,71,60,0,0,0,0,0,0,17,0,0,0,0,0),
                              "H_total"=c(89.7,109.6,15.4,140.1,145.4,164,11.6,118.1,110.8,50.4,77.8,124.6,115.6,152,63.6,112.9,127.7,138.5,69.8,149.4,32,63.4,35.7,84.5,5.8,0,8.8,1.6,20.2,31.8,25.8,0.5),
                              "B1_fs"=c(0,0,0.5,0,0.5,0,3,0,0,0.5,2,10,0,0,7,0,0,20,15,18,75,71,115,72,65,95,40,35,43,96,98,95),
                              "S_total"=c(8.6,8.5,2.2,18.9,37.9,53.1,3.8,76,67.4,4.8,35.2,78,74.5,65.6,41.6,58,37.1,60.7,68,39.5,44.4,69.6,5.5,42.1,19.3,0,77.1,0.5,96.2,2.0,8.5,0.5))



    ggplot(data=Forest_EPs_pure, aes(x=Sand))+
      geom_point(aes(y=B2_fs), color="green")+
      geom_point(aes(y=H_total), color="black")+
      geom_point(aes(y=B1_fs), color="brown")+
      geom_point(aes(y=S_total), color="grey")+
      geom_smooth(aes(y=B2_fs), method="glm",color="green")+
      geom_smooth(aes(y=H_total), method="glm", color="black")+
      geom_smooth(aes(y=B1_fs), method="glm", color="brown")+
      geom_smooth(aes(y=S_total), method="glm", color="grey")+
      scale_x_continuous("Sand content in g/kg")+
      scale_y_continuous("Share of respective layer in %")

1 个答案:

答案 0 :(得分:1)

ggplot通常期望长格式的数据。您可以使用gather中的tidyr来实现此目的。

例如,使用此数据,您将拥有一个包含&#34; B1_fs&#34;,&#34; B2_fs&#34;,&#34; H_total&#34;和&#34; S_total&#的列34;或您在重塑数据之前可能拥有的任何其他列。这允许您将该变量映射到颜色,这将为您提供图例,并使用scale_color_manual中的命名向量手动设置颜色。

一般情况下,您不希望使用相同类型的多个geom来执行类似操作,例如重复使用geom_point来绘制不同颜色的点。

library(ggplot2)

Forest_EPs_pure <- data.frame("Sand"=c(23,41,32,34,38,49,32,18,91,18,117,61,68,43,46,59,74,88,58,92,882,941,870,926,861,848,810,964,874,860,755,942),
                                                            "B2_fs"=c(61,68,80,20,43,72,93,60,75,75,97,83,74,51,87.5,74,97,74,71,60,0,0,0,0,0,0,17,0,0,0,0,0),
                                                            "H_total"=c(89.7,109.6,15.4,140.1,145.4,164,11.6,118.1,110.8,50.4,77.8,124.6,115.6,152,63.6,112.9,127.7,138.5,69.8,149.4,32,63.4,35.7,84.5,5.8,0,8.8,1.6,20.2,31.8,25.8,0.5),
                                                            "B1_fs"=c(0,0,0.5,0,0.5,0,3,0,0,0.5,2,10,0,0,7,0,0,20,15,18,75,71,115,72,65,95,40,35,43,96,98,95),
                                                            "S_total"=c(8.6,8.5,2.2,18.9,37.9,53.1,3.8,76,67.4,4.8,35.2,78,74.5,65.6,41.6,58,37.1,60.7,68,39.5,44.4,69.6,5.5,42.1,19.3,0,77.1,0.5,96.2,2.0,8.5,0.5))

forest_long <- tidyr::gather(Forest_EPs_pure, key = variable, value = value, -Sand)
head(forest_long)
#>   Sand variable value
#> 1   23    B2_fs    61
#> 2   41    B2_fs    68
#> 3   32    B2_fs    80
#> 4   34    B2_fs    20
#> 5   38    B2_fs    43
#> 6   49    B2_fs    72

ggplot(forest_long, aes(x = Sand, y = value, color = variable)) +
    geom_point() +
    geom_smooth(method = glm) +
    scale_color_manual(values = c(B2_fs = "green", H_total = "black", B1_fs = "brown", S_total = "grey"))

reprex package(v0.2.0)创建于2018-05-10。