0

I'm trying to plot 4 regressions, all having the same predictor, with according points in 4 different colors. I'd need a legend that assigns 4 labels to the 4 colors. Merely just a simple table with 2 columns. However when trying to set a manual with scale_linetype_manualor scale_fill_manual, no legend appears.

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 Answers1

1

ggplot generally expects data in a long format. You can use gather from tidyr for this.

For example, with this data you would have a column that contains "B1_fs", "B2_fs", "H_total", and "S_total", or any other columns you may have before reshaping the data. That allows you to map that variable onto color, which gets you a legend, and set the colors manually with a named vector in scale_color_manual.

In general, you don't want to use multiple geoms of the same type to do something similar, such as using geom_point repeatedly to plot points in different colors.

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"))

Created on 2018-05-10 by the reprex package (v0.2.0).

camille
  • 16,432
  • 18
  • 38
  • 60