2

I have to place my legend horizontally above the barplot. I have search a lot but could not found a statisfactory answer. I have found an answer here but it does not solve my problem.

Community
  • 1
  • 1
Hafiz Muhammad Shafiq
  • 8,168
  • 12
  • 63
  • 121

1 Answers1

6

I don't know what your data looks like and what you want your legend to be but horizontal legends are produced by setting horiz = TRUE. You can place a legend on the top of a plot using "top" as the legend position. If you want it outside your plot region you can move it upward using inset (you'll have to play around with the value a bit) and setting xpd = TRUE (which basically allows you to write outside the plot region):

Example:

barplot(c(10,2,7), col = 1:3) 
legend("top", fill = 1:3, legend = c("A", "B", "C"), 
    horiz = TRUE, inset = c(0,-0.1), xpd = TRUE)

produces the following graph:

barplot with horizontal legend on top

enter image description here

user20650
  • 24,654
  • 5
  • 56
  • 91
ikop
  • 1,760
  • 1
  • 12
  • 24