Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a legend without a plot in R?

This is for an art project. I've created a scatterplot with many dots of 5 different colours. I want to create a legend that is completely separate from the plot, as in it is not on the plot, nor beside the plot, but is in it's own window so I can save the legend as it's own .pdf file. This is so I can have my plot and legend printed separately as they well be hung in a gallery as separate pieces.

All I want is to recreate this image using R:

enter image description here

Is this even possible?

Thanks,

Jay

like image 587
Jay Avatar asked Feb 24 '18 19:02

Jay


People also ask

How do you make a legend outside the plot in R?

In order to draw our legend outside of the plotting area, we can use a combination of the “topright” argument and an additional specification of inset. The “topright” argument specifies that the legend should be in the upper right corner of the graph.

How do I add a legend to a Barplot in R?

In R you can add a legend to any plot using the legend() command. You can also use the legend = TRUE parameter in the barplot() command. The barplot() command is the only general plot type that has a legend parameter (the others need a separate legend).

What is the use of legend () function in R?

legend() function in R Programming Language is used to add legends to an existing Plot.


1 Answers

Sure. Just start from an empty plot and then use legend as you would if there were a plot.

plot(NULL ,xaxt='n',yaxt='n',bty='n',ylab='',xlab='', xlim=0:1, ylim=0:1)
legend("topleft", legend =c('Sugar maple', 'White ash', 'Black walnut',
    'Red oak', 'Eastern hemlock'), pch=16, pt.cex=3, cex=1.5, bty='n',
    col = c('orange', 'red', 'green', 'blue', 'purple'))
mtext("Species", at=0.2, cex=2)

Legend without a plot

like image 85
G5W Avatar answered Oct 09 '22 13:10

G5W