将图例添加到 ggplot2线条图

我有一个关于 ggplot2中传说的问题。我设法在同一个图形中绘制了三条线,并希望添加一个使用了三种颜色的图例。这是所用的代码

library(ggplot2)


## edit from original post - removed lines that downloaded data from broken link. Data snippet now below.
## Here a subset as used by [Brian Diggs in their answer](https://stackoverflow.com/a/10355844/7941188)
datos <- structure(list(fecha = structure(c(1317452400, 1317538800, 1317625200, 1317711600, 1317798000, 1317884400, 1317970800, 1318057200, 1318143600, 1318230000, 1318316400, 1318402800, 1318489200, 1318575600, 1318662000, 1318748400, 1318834800, 1318921200, 1319007600, 1319094000), class = c("POSIXct", "POSIXt"), tzone = ""), TempMax = c(26.58, 27.78, 27.9, 27.44, 30.9, 30.44, 27.57, 25.71, 25.98, 26.84, 33.58, 30.7, 31.3, 27.18, 26.58, 26.18, 25.19, 24.19, 27.65, 23.92), TempMedia = c(22.88, 22.87, 22.41, 21.63, 22.43, 22.29, 21.89, 20.52, 19.71, 20.73, 23.51, 23.13, 22.95, 21.95, 21.91, 20.72, 20.45, 19.42, 19.97, 19.61), TempMin = c(
19.34, 19.14, 18.34, 17.49, 16.75, 16.75, 16.88, 16.82, 14.82, 16.01, 16.88, 17.55, 16.75, 17.22, 19.01,
16.95, 17.55, 15.21, 14.22, 16.42
)), .Names = c(
"fecha", "TempMax",
"TempMedia", "TempMin"
), row.names = c(NA, 20L), class = "data.frame")


ggplot(data = datos, aes(x = fecha, y = TempMax, colour = "1")) +
geom_line(colour = "red") +
geom_line(aes(x = fecha, y = TempMedia, colour = "2"), colour = "green") +
geom_line(aes(x = fecha, y = TempMin, colour = "2"), colour = "blue") +
scale_y_continuous(limits = c(-10, 40)) +
scale_colour_manual(values = c("red", "green", "blue")) +
labs(title = "TITULO", x = NULL, y = "Temperatura (C)")


我想添加一个图例与使用的三种颜色和变量的名称(TempMax,TempMedia 和 TempMin)。我试过了 scale_colour_manual,但找不到确切的方式。

371523 次浏览

我倾向于发现,如果我在多个宝石中指定单独的颜色,我就做错了。下面是我如何绘制你的数据:

library(ggplot2)
library(tidyr)


dd_tidyr <- pivot_longer(datos, cols = -fecha, names_to = "Temperature")

剩下的只是一个简单的 ggplot 命令:

ggplot(dd_tidyr) +
geom_line(aes(x = fecha, y = value, colour = Temperature)) +
scale_colour_manual(values = c("red", "green", "blue"))

还有其他重新设置数据形状的方法。替代的方法是使用现在已经退役的{ respe2}包。

library(reshape2)
dd = melt(datos, id=c("fecha"))

由于 @ Etienne询问如何在不熔化数据的情况下完成此操作(这通常是首选的方法,但我认识到在某些情况下这是不可能的) ,因此我提出以下备选方案。

从原始数据的子集开始:

datos <-
structure(list(fecha = structure(c(1317452400, 1317538800, 1317625200,
1317711600, 1317798000, 1317884400, 1317970800, 1318057200, 1318143600,
1318230000, 1318316400, 1318402800, 1318489200, 1318575600, 1318662000,
1318748400, 1318834800, 1318921200, 1319007600, 1319094000), class = c("POSIXct",
"POSIXt"), tzone = ""), TempMax = c(26.58, 27.78, 27.9, 27.44,
30.9, 30.44, 27.57, 25.71, 25.98, 26.84, 33.58, 30.7, 31.3, 27.18,
26.58, 26.18, 25.19, 24.19, 27.65, 23.92), TempMedia = c(22.88,
22.87, 22.41, 21.63, 22.43, 22.29, 21.89, 20.52, 19.71, 20.73,
23.51, 23.13, 22.95, 21.95, 21.91, 20.72, 20.45, 19.42, 19.97,
19.61), TempMin = c(19.34, 19.14, 18.34, 17.49, 16.75, 16.75,
16.88, 16.82, 14.82, 16.01, 16.88, 17.55, 16.75, 17.22, 19.01,
16.95, 17.55, 15.21, 14.22, 16.42)), .Names = c("fecha", "TempMax",
"TempMedia", "TempMin"), row.names = c(NA, 20L), class = "data.frame")

您可以通过以下方式获得所需的效果(这也清除了原始绘图代码) :

ggplot(data = datos, aes(x = fecha)) +
geom_line(aes(y = TempMax, colour = "TempMax")) +
geom_line(aes(y = TempMedia, colour = "TempMedia")) +
geom_line(aes(y = TempMin, colour = "TempMin")) +
scale_colour_manual("",
breaks = c("TempMax", "TempMedia", "TempMin"),
values = c("red", "green", "blue")) +
xlab(" ") +
scale_y_continuous("Temperatura (C)", limits = c(-10,40)) +
labs(title="TITULO")

其思想是,通过将 colour美学映射到一个常量字符串,为每一行赋予一种颜色。选择你想在图例中出现的字符串是最简单的。在这种情况下,它与所绘制的 y变量的名称相同的事实并不重要; 它可以是任何字符串集。这一点非常重要,因为它位于 aes调用内部; 您正在创建一个到这个“变量”的映射。

scale_colour_manual现在可以将这些字符串映射到适当的颜色 enter image description here

在某些情况下,水平和颜色之间的映射需要通过在手动刻度中命名值来明确(感谢 @ DaveRGP指出这一点) :

ggplot(data = datos, aes(x = fecha)) +
geom_line(aes(y = TempMax, colour = "TempMax")) +
geom_line(aes(y = TempMedia, colour = "TempMedia")) +
geom_line(aes(y = TempMin, colour = "TempMin")) +
scale_colour_manual("",
values = c("TempMedia"="green", "TempMax"="red",
"TempMin"="blue")) +
xlab(" ") +
scale_y_continuous("Temperatura (C)", limits = c(-10,40)) +
labs(title="TITULO")

(给出与以前相同的数字)。对于命名值,可以使用中断来设置图例中的顺序,并且可以在值中使用任何顺序。

ggplot(data = datos, aes(x = fecha)) +
geom_line(aes(y = TempMax, colour = "TempMax")) +
geom_line(aes(y = TempMedia, colour = "TempMedia")) +
geom_line(aes(y = TempMin, colour = "TempMin")) +
scale_colour_manual("",
breaks = c("TempMedia", "TempMax", "TempMin"),
values = c("TempMedia"="green", "TempMax"="red",
"TempMin"="blue")) +
xlab(" ") +
scale_y_continuous("Temperatura (C)", limits = c(-10,40)) +
labs(title="TITULO")

我真的很喜欢@Brian Diggs 提出的解决方案。然而,在我的例子中,我在一个循环中创建线性图,而不是明确地给出它们,因为我先验地不知道我将拥有多少图。当我试图改编@Brian 的代码时,我遇到了一些正确处理颜色的问题。原来我需要修改审美功能。如果有人遇到同样的问题,这里是我使用过的代码。

我使用了与@Brian 相同的数据框架:

data <- structure(list(month = structure(c(1317452400, 1317538800, 1317625200, 1317711600,
1317798000, 1317884400, 1317970800, 1318057200,
1318143600, 1318230000, 1318316400, 1318402800,
1318489200, 1318575600, 1318662000, 1318748400,
1318834800, 1318921200, 1319007600, 1319094000),
class = c("POSIXct", "POSIXt"), tzone = ""),
TempMax = c(26.58, 27.78, 27.9, 27.44, 30.9, 30.44, 27.57, 25.71,
25.98, 26.84, 33.58, 30.7, 31.3, 27.18, 26.58, 26.18,
25.19, 24.19, 27.65, 23.92),
TempMed = c(22.88, 22.87, 22.41, 21.63, 22.43, 22.29, 21.89, 20.52,
19.71, 20.73, 23.51, 23.13, 22.95, 21.95, 21.91, 20.72,
20.45, 19.42, 19.97, 19.61),
TempMin = c(19.34, 19.14, 18.34, 17.49, 16.75, 16.75, 16.88, 16.82,
14.82, 16.01, 16.88, 17.55, 16.75, 17.22, 19.01, 16.95,
17.55, 15.21, 14.22, 16.42)),
.Names = c("month", "TempMax", "TempMed", "TempMin"),
row.names = c(NA, 20L), class = "data.frame")

在我的例子中,我动态地生成 my.colsmy.names,但是我不想让事情变得不必要的复杂,所以我在这里明确地给出它们。这三条线使图例的排序和分配颜色更加容易。

my.cols <- heat.colors(3, alpha=1)
my.names <- c("TempMin", "TempMed", "TempMax")
names(my.cols) <- my.names

情节是这样的:

p <-  ggplot(data, aes(x = month))


for (i in 1:3){
p <- p + geom_line(aes_(y = as.name(names(data[i+1])), colour =
colnames(data[i+1])))#as.character(my.names[i])))
}
p + scale_colour_manual("",
breaks = as.character(my.names),
values = my.cols)
p

enter image description here

使用 scale_color_identity的另一个选项。关键是在每个 geom_line的每个 aes中指定颜色名称。在 scale_color_identity你可以提到每个颜色作为 breaks和添加的 labels你想要的。下面是一个可重复的例子:

library(ggplot2)


temp = ggplot(data = datos, aes(x = fecha)) +
geom_line(aes(y = TempMax, colour = "red")) +
geom_line(aes(y = TempMedia, colour = "green")) +
geom_line(aes(y = TempMin, colour = "blue")) +
labs(title="TITULO") +
ylab("Temperatura (C)") +
xlab(" ") +
scale_y_continuous(limits = c(-10,40)) +
scale_color_identity(name = '',
breaks = c('red', 'green', 'blue'),
labels = c("TempMax", "TempMedia", "TempMin"),
guide = 'legend')


temp

创建于2022-11-19与 Reprex v2.0.2