将图例添加到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.42000-08-01 00:00:00,22.9,19,21.11,-99.9,6.3,2.832000-08-03 00:00:00,24.8,12.3,19.23,-99.9,6.8,3.872000-08-04 00:00:00,20.3,9.4,14.4,-99.9,8.3,5.292000-08-08 00:00:00,25.7,14.4,19.5,-99.9,7.9,3.222000-08-09 00:00:00,29.8,16.2,22.14,-99.9,8.5,3.272000-08-10 00:00:00,30,17.8,23.5,-99.9,7.7,3.612000-08-11 00:00:00,27.5,17,22.68,-99.9,8.8,3.852000-08-12 00:00:00,24,13.3,17.32,-99.9,8.4,3.49
3 回答
鸿蒙传说
TA贡献1865条经验 获得超7个赞
##Subset the necessary columnsdd_sub = datos[,c(20, 2,3,5)]##Then rearrange your data framelibrary(reshape2)dd = melt(dd_sub, id=c("fecha"))
ggplot(dd) + geom_line(aes(x=fecha, y=value, colour=variable)) + scale_colour_manual(values=c("red","green","blue"))
示例图
- 3 回答
- 0 关注
- 694 浏览
添加回答
举报
0/150
提交
取消