Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding simple legend to plot in R

Tags:

r

I would like to create a very simple plot. I am using this data:

a <- read.table("https://dl.dropbox.com/u/22681355/a.csv", sep=',', header=TRUE)

plot(a$X25, type="l",col="red", ylim=c(0,100))
lines(a$X25.1, type="l", col="blue")
lines(a$X25.2, type="l", col="green")
lines(a$X25.3, type="l", col="brown")

Now I would like to add a simple legend that indicates which color is which variable.

I understand that I can use the legend() command, but my problem is that I don't know how to put colors next to the text in the legend.

What's the simplest command that would do this?

like image 767
user1723765 Avatar asked Feb 14 '13 20:02

user1723765


People also ask

How do I add a legend to a Barplot in R?

In R you can add a legend to any plot using the legend() command. You can also use the legend = TRUE parameter in the barplot() command. The barplot() command is the only general plot type that has a legend parameter (the others need a separate legend).

What is the use of legend () function in R?

legend() function in R Programming Language is used to add legends to an existing Plot.


Video Answer


1 Answers

Take a look at ?legend and try this:

legend('topright', names(a)[-1] , 
   lty=1, col=c('red', 'blue', 'green',' brown'), bty='n', cex=.75)

enter image description here

like image 134
Jilber Urbina Avatar answered Oct 28 '22 07:10

Jilber Urbina