logo

How to Insert a Legend in R 📂R

How to Insert a Legend in R

Code

3.png Data representation is just as important as data analysis. The more complex a figure becomes, the more meticulous annotations and clear legends help in understanding the data. The legend() function has a plethora of options, yet employing just the essential elements as below is beneficial.

The first option for location can simply be input as a combination of “top”, “bottom” + “left”, “right”, or “center”, and specific coordinates can also be given if preferred.

win.graph(5,5)
plot(iris<i>$</i>Sepal.Length,iris$Sepal.Width,main="Iris")
points(iris<i>$</i>Sepal.Length,iris$Sepal.Width,pch=16,col=as.numeric(iris$Species))
legend("topright",pch=16,col=1:3,legend=unique(iris$Species))