将图例添加到ggplot 2行图中

将图例添加到ggplot 2行图中

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


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

以及输出


ggplot three lines


我想添加一个图例和使用的三种颜色和变量的名称(TempMax,TempMedia和TempMin)。我试过了


scale_colour_manual

但找不到确切的路。


不幸的是,原始数据已从链接站点中删除,无法恢复。但它们来自于这种格式的meteo数据文件。


"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


慕少森
浏览 659回答 3
3回答

鸿蒙传说

我倾向于发现,如果我在多个geom‘s中指定单个颜色,我就做错了。以下是我如何绘制您的数据:##Subset&nbsp;the&nbsp;necessary&nbsp;columnsdd_sub&nbsp;=&nbsp;datos[,c(20,&nbsp;2,3,5)]##Then&nbsp;rearrange&nbsp;your&nbsp;data&nbsp;framelibrary(reshape2)dd&nbsp;=&nbsp;melt(dd_sub,&nbsp;id=c("fecha"))剩下的只是一个简单的ggplote命令:ggplot(dd)&nbsp;+&nbsp;geom_line(aes(x=fecha,&nbsp;y=value,&nbsp;colour=variable))&nbsp;+ &nbsp;&nbsp;scale_colour_manual(values=c("red","green","blue"))示例图
打开App,查看更多内容
随时随地看视频慕课网APP