Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R : legend() error

Tags:

plot

r

legend

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?

like image 327
screechOwl Avatar asked Sep 13 '25 01:09

screechOwl


1 Answers

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.

like image 131
puslet88 Avatar answered Sep 15 '25 14:09

puslet88