R : legend() error

22,530

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.

Share:
22,530
screechOwl
Author by

screechOwl

https://financenerd.blog/blog/

Updated on March 12, 2020

Comments

  • screechOwl
    screechOwl about 4 years

    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?