0
votes

I am having to use base R and I am only really familiar with ggplot2. I have told R to colour my plot by site, which it has done... but unlike ggplot2 it doesn't automatically give me a legend to tell me which colour it has assigned to which site. Is there either a way to tell R to plot a site in a specific colour or a way to make it create a legend. I tried creating a legend but it seemed I had to input exactly what colour, shape etc should be on the legend which defeats the point.

Thanks.

plot(mod1.xval, xval=TRUE, resid=TRUE, xlab="Observed elevation (m AHD)", ylab= "Inferred elevation (m AHD)",npls=2,col=spec$Site)


1
"I tried creating a legend but it seemed I had to input exactly what colour, shape etc should be on the legend which defeats the point." That's why ggplot2 (and lattice earlier) became so popular. Unfortunately, base graphics require you to create the legend manually. - Roland
Oh no! That is super annoying - how on Earth do people work out what their graphs are actually plotting! - Sophie Williams
On the flip site, you can customize plots more easily than with ggplot2. - Roland

1 Answers

1
votes

Here is an example with the iris data set. Species would be your Site. You define the color for Species and then build the legend, in which the colors are made with unique(iris$Species)) .

plot(Sepal.Length~ Sepal.Width, iris, col=Species, pch=16)
legend('topleft', col=unique(iris$Species), legend=levels(iris$Species), pch =15)