简体   繁体   English

将图例添加到ggplot2

[英]Adding a legend to a ggplot2

I try to add a legend to my ggplot2 plots but it is not working out. 我尝试在我的ggplot2图中添加一个图例,但它没有用完。 Also the command show.legend does not change anything show.legend命令也不会改变任何东西

Following the answer of this question ( Adding manual legend in ggplot ) I tried scal_colour_manueal but it did not work out 在这个问题的答案之后( 在ggplot中添加手动图例 )我尝试了scal_colour_manueal,但它没有成功

library(ggplot2)
library(data.table)

color1 = "#D30F4B"
color2 = "#66B512"

data= data.frame(Week = rep(1:5,2), kpi = rep(c("Var1", "Var2"), each=5), value = runif(10), value2 = c(runif(5), rep(NA,5))  )

ggp <- ggplot( data = data, aes( x = Week, y = value, group = kpi) ) +
  geom_line(color=color1, show.legend = T) 

ggp <- ggp +
geom_line( mapping = aes( x = Week, y = value2, group = kpi), colour = color2 , show.legend = T) 

ggp <- ggp +  
  facet_wrap( kpi ~ . , ncol = 1) +
  scale_colour_manual(name="Legend", values=c(color1, color2))

plot(ggp)

How can I add a legend to this plot? 如何在此图中添加图例?

You shouldn't set colours outside of aes if you want them to depent on some variable of the data frame. 如果您希望它们依赖于数据框的某个变量,则不应在aes之外设置颜色。

library(ggplot2)
library(data.table)

color1 = "#D30F4B"
color2 = "#66B512"

data= data.frame(Week = rep(1:5,2), kpi = rep(c("Var1", "Var2"), each=5), value = runif(10), value2 = c(runif(5), rep(NA,5))  )

ggp <- ggplot( data = data, aes( x = Week, y = value, col= kpi) ) +
  geom_line(show.legend = T) 

ggp <- ggp +
geom_line( mapping = aes( x = Week, y = value2, col= kpi), show.legend = T) 

ggp <- ggp +  
  facet_wrap( kpi ~ . , ncol = 1) +
  scale_colour_manual(name="Legend", values=c(color1, color2))

plot(ggp)

情节

Another hint: ggplot works best, when data is "tidy" (see eg Hadley Wickham's R for Data Science ). 另一个暗示:当数据“整洁”时,ggplot效果最好(参见例如Hadley Wickham的R for Data Science )。 Then solution could look like this (the gather -call reshapes / tidies the data so that it suits better to ggplot): 然后解决方案看起来像这样( gather调用重新整形/整理数据,以便它更适合ggplot):

library(tidyverse)
library(ggplot2)
library(data.table)

color1 = "#D30F4B"
color2 = "#66B512"

data= data.frame(Week = rep(1:5,2), kpi = rep(c("Var1", "Var2"), each=5), value = runif(10), value2 = c(runif(5), rep(NA,5))  )

data <- gather(data, key = value_name, value = value, -Week, -kpi) ## tidy the data with "gahter"

ggplot(data, aes(x = Week, y = value, colour = value_name, group = value_name)) +
    geom_line() +
    facet_wrap(kpi ~ ., ncol = 1) +
    scale_colour_manual(name="Legend", values=c(color1, color2))

We could have two geom_line with their respective colors and then use facet_wrap 我们可以有两个geom_line各自的颜色,然后用facet_wrap

library(ggplot2)

ggplot(data) + 
  aes(x = Week, y = value2, group = kpi, colour = color2) +
  geom_line() +
  geom_line(aes( x = Week, y = value, group = kpi, color = color1)) + 
  facet_wrap( kpi ~ . , ncol = 1) +
  scale_colour_manual(name="Legend", values=c(color1, color2))

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM