Легенда для GGPlot Grob - PullRequest
       34

Легенда для GGPlot Grob

0 голосов
/ 20 октября 2019
library(ggplot2)
library(gridExtra)
data(iris)
iris$group=sample(0:1,rep=T)

g1 = ggplot() + geom_line(data=subset(iris, group == 0, Species == "setosa"), aes(Sepal.Length, Sepal.Width), color = "blue", linetype = "solid") + 
  geom_line(data=subset(iris, group == 0, Species == "setosa"), aes(Petal.Length, Petal.Width), color = "blue", linetype = "dashed") +
  geom_line(data=subset(iris, group == 0, Species == "versicolor"), aes(Sepal.Length, Sepal.Width), color = "red", linetype = "solid") 
  geom_line(data=subset(iris, group == 0, Species == "versicolor"), aes(Petal.Length, Petal.Width), color = "red", linetype = "dashed")



  g2 = ggplot() + geom_line(data=subset(iris, group == 1, Species == "setosa"), aes(Sepal.Length, Sepal.Width), color = "blue", linetype = "solid") + 
    geom_line(data=subset(iris, group == 1, Species == "setosa"), aes(Petal.Length, Petal.Width), color = "blue", linetype = "dashed") +
    geom_line(data=subset(iris, group == 1, Species == "versicolor"), aes(Sepal.Length, Sepal.Width), color = "red", linetype = "solid") 
  geom_line(data=subset(iris, group == 1, Species == "versicolor"), aes(Petal.Length, Petal.Width), color = "red", linetype = "dashed")



  g <- arrangeGrob(g1,g2, legend, ncol=1)

Я придумаю код выше. Моя цель - сложить эти два графика g1 и g2. А потом есть одна легенда в самом низу. Я хочу показать цвета и указанные переменные, а также отдельно тип линии. Таким образом, легенда должна содержать ключ, который показывает:

blue = setosa
red = versicolor
solid = sepal
dashed = petal
...