I'm trying to generate a legend for a plot to show the colors' relationships to cluster # in a plot. I don't actually need it in the plot I just want to generate a legend then copy and paste it into a powerpoint slide.
I found this code here that does what I want:
http://www.statmethods.net/advgraphs/axes.html
# Legend Example
attach(mtcars)
boxplot(mpg~cyl, main="Milage by Car Weight",
yaxt="n", xlab="Milage", horizontal=TRUE,
col=terrain.colors(3))
legend("topright", inset=.05, title="Number of Cylinders",
c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)
but am having difficulty replicating it. Here's my code:
plot(seq(1,7), seq(1,7), col = c(1:7))
legend("topright", inset = .05, title = "Cluster Colors"
,fill = c(1:7), horiz=T)
when I run it, I get this error:
Error in as.graphicsAnnot(legend) :
argument "legend" is missing, with no default
Any suggestions?
The original plot has an argument "legend", it is just unnamed argument. As updated here:
legend("topright", inset=.05, title="Number of Cylinders",
legend =c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)
So, what you need is this.
plot(seq(1,7), seq(1,7), col = c(1:7))
legend("topright", inset = .05, title = "Cluster Colors",legend= c(1:7)
,fill = c(1:7), horiz=TRUE)
The ?
command, as in ?legend
, is useful to find out about these things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With