Search code examples
rplotlegendfigure

set the size of figure legend in R


everyone. I am using plot function in R studio. Below is my plot. As you can see, the figure legend box is too big, I want to set it smaller. I have tried to modify same parameters, but it didn't work. my code is:

legend("topleft", legend=names(attr(colcode, "table")), 
       fill=attr(colcode, "palette"), cex=0.6,bg = "white", text.width = 1)

enter image description here


Solution

  • You specified text.width = 1 which on your graph is the entire width of the graph. It will look OK if you reduce text.width. Try

    legend("topleft", legend=names(attr(colcode, "table")), 
           fill=attr(colcode, "palette"), cex=0.6,bg = "white", text.width = 0.1)
    

    You may need to reduce it even more.