Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd legend to ggplot2 line plot
    primarykey
    data
    text
    <p>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</p> <pre><code>library(ggplot2) require(RCurl) link&lt;-getURL("https://dl.dropbox.com/s/ds5zp9jonznpuwb/dat.txt") datos&lt;- read.csv(textConnection(link),header=TRUE,sep=";") datos$fecha &lt;- 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 </code></pre> <p>and the output</p> <p><img src="https://i.stack.imgur.com/4LqYa.png" alt="ggplot three lines"></p> <p>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 </p> <pre><code>scale_colour_manual </code></pre> <p>but can't find the exact way.</p> <p>Unfortunately original data were deleted from linked site and could not be recovered. But they came from meteo data files with this format</p> <pre><code>"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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload