0

my very sad bargraph

enter image description here

legend("topright", c("No Response", "Correct", "Incorrect"), 
        fill=c("#999999", "#33FF33", "#FF0000"))
legend("bottomright", c("No Response", "Correct", "Incorrect"), 
       fill=c("#999999", "#33FF33", "#FF0000"))

When I try to make it smaller I use cex=0.75, but all that does is make the legend smaller, it still sits on top of the data.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Does this answer your question? [Plot a legend outside of the plotting area in base graphics?](https://stackoverflow.com/questions/3932038/plot-a-legend-outside-of-the-plotting-area-in-base-graphics) – Paul Sep 21 '20 at 06:03
  • Thank you for your help!! I got it to work!!!!! – Natalie David Sep 24 '20 at 23:22

1 Answers1

3

Base graphics doesn't naturally make room for legends and such (perhaps because it does not know you're planning on adding one). You can use xlim (and/or ylim) of the original plot in order to make room for it.

barplot(GNP ~ Year, data = longley)
legend("bottomright", c("No Response", "Correct", "Incorrect"), 
       fill=c("#999999", "#33FF33", "#FF0000"))

barplot, legend overlaid on data

par("usr")[1:2]
# [1] -0.56 19.96
barplot(GNP ~ Year, data = longley, xlim = c(0, 26))
legend("bottomright", c("No Response", "Correct", "Incorrect"), 
       fill=c("#999999", "#33FF33", "#FF0000"))

barplot, legend placed better

r2evans
  • 141,215
  • 6
  • 77
  • 149