最佳答案
我有一个关于 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
,但找不到确切的方式。