将图例添加到ggplot2线图 [英] Add legend to ggplot2 line plot
问题描述
我有一个关于ggplot2中的传说的问题。我设法在同一个图中绘制三条线,并希望添加使用三种颜色的图例。这是使用的代码
library(ggplot2)
require(RCurl)
link< ; -getURL(https://dl.dropbox.com/s/ds5zp9jonznpuwb/dat.txt)
datos< - read.csv(textConnection(link),header = TRUE,sep =;)
datos $ fecha< - as.POSIXct(datos [,1],format =%d /%m /%Y)
temp = ggplot(data = datos,aes (x = fecha,y = TempMax,color =1))+
geom_line(color =red)+ opts(title =TITULO)+
ylab(Temperatura(C) )+ xlab()+
scale_y_continuous(limits = c(-10,40))+
geom_line(aes(x = fecha,y = TempMedia,color =2),color =green)+
geom_line(aes(x = fecha,y = TempMin,color =2),color =blue)+
scale_colour_manual(values = c(red green,blue))
temp
和输出
我想添加三个图例使用的颜色和变量的名称(TempMax,TempMedia和TempMin)。我曾尝试
scale_colour_manual
,但找不到确切的方式。
不幸的是,原始数据已从链接网站中删除,无法恢复。但他们来自这种格式的气象数据文件
date,Tmax,Tmin,Tmed, Precip.diaria,Wmax,Wmed
2000-07-31 00:00:00,-1.7,-1.7,-1.7,-99.9,20.4,20.4
2000-08 -01 00:00:00,22.9,19,21.11,-99.9,6.3,2.83
2000-08-03 00:00:00,24.8,12.3,19.23,-99.9,6.8,3.87
2000-08-04 00:00:00,20.3,9.4,14.4,-99.9,8.3,5.29
2000-08-08 00:00:00,25.7,14.4,19.5,-99.9,7.9, 3.22
2000-08-09 00:00:00,29.8,16.2,22.14,-99.9,8.5,3.27
2000-08-10 00:00:00,30,17.8,23.5, - 99.9,7.7,3.61
2000-08-11 00:00:00,27.5,17,22.68,-99.9,8.8,3.85
2000-08-12 00:00:00,24,13.3 ,17.32,-99.9,8.4,3.49
发现如果我在多个几何图形中指定单独的颜色,我做错了。以下是我如何绘制数据:
##子集所需列
dd_sub = datos [,c(20 ,2,3,5)]
##然后重新排列你的数据框
library(reshape2)
dd = melt(dd_sub,id = c(fecha))
剩下的就是一个简单的ggplot命令:
<$ p $ ($ x $ f $ g $)$ g $($)$ g $($)蓝色))
示例图
I have a question about legends in ggplot2. I managed to plot three lines in the same graph and want to add a legend with the three colors used. This is the code used
library(ggplot2)
require(RCurl)
link<-getURL("https://dl.dropbox.com/s/ds5zp9jonznpuwb/dat.txt")
datos<- read.csv(textConnection(link),header=TRUE,sep=";")
datos$fecha <- as.POSIXct(datos[,1], format="%d/%m/%Y")
temp = ggplot(data=datos,aes(x=fecha, y=TempMax,colour="1")) +
geom_line(colour="red") + opts(title="TITULO") +
ylab("Temperatura (C)") + xlab(" ") +
scale_y_continuous(limits = c(-10,40)) +
geom_line(aes(x=fecha, y=TempMedia,colour="2"),colour="green") +
geom_line(aes(x=fecha, y=TempMin,colour="2"),colour="blue") +
scale_colour_manual(values=c("red","green","blue"))
temp
and the output
I'd like to add a legend with the three colours used and the name of the variable (TempMax,TempMedia and TempMin). I have tried
scale_colour_manual
but can't find the exact way.
Unfortunately original data were deleted from linked site and could not be recovered. But they came from meteo data files with this format
"date","Tmax","Tmin","Tmed","Precip.diaria","Wmax","Wmed"
2000-07-31 00:00:00,-1.7,-1.7,-1.7,-99.9,20.4,20.4
2000-08-01 00:00:00,22.9,19,21.11,-99.9,6.3,2.83
2000-08-03 00:00:00,24.8,12.3,19.23,-99.9,6.8,3.87
2000-08-04 00:00:00,20.3,9.4,14.4,-99.9,8.3,5.29
2000-08-08 00:00:00,25.7,14.4,19.5,-99.9,7.9,3.22
2000-08-09 00:00:00,29.8,16.2,22.14,-99.9,8.5,3.27
2000-08-10 00:00:00,30,17.8,23.5,-99.9,7.7,3.61
2000-08-11 00:00:00,27.5,17,22.68,-99.9,8.8,3.85
2000-08-12 00:00:00,24,13.3,17.32,-99.9,8.4,3.49
I tend to find that if I'm specifying individual colours in multiple geom's, I'm doing it wrong. Here's how I would plot your data:
##Subset the necessary columns
dd_sub = datos[,c(20, 2,3,5)]
##Then rearrange your data frame
library(reshape2)
dd = melt(dd_sub, id=c("fecha"))
All that's left is a simple ggplot command:
ggplot(dd) + geom_line(aes(x=fecha, y=value, colour=variable)) +
scale_colour_manual(values=c("red","green","blue"))
Example plot
这篇关于将图例添加到ggplot2线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!