亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將圖例添加到ggplot 2行圖中

將圖例添加到ggplot 2行圖中

慕少森 2019-06-10 16:31:01
將圖例添加到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個贊

我傾向于發現,如果我在多個geom‘s中指定單個顏色,我就做錯了。以下是我如何繪制您的數據:

##Subset the necessary columnsdd_sub = datos[,c(20, 2,3,5)]##Then rearrange your data framelibrary(reshape2)dd = melt(dd_sub, id=c("fecha"))

剩下的只是一個簡單的ggplote命令:

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

示例圖


查看完整回答
反對 回復 2019-06-10
  • 3 回答
  • 0 關注
  • 726 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號