Hafiz Muhammad Shafiq
Hafiz Muhammad Shafiq

Reputation: 8680

how to place legend horizontaly above barplot in r

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.

Upvotes: 2

Views: 4787

Answers (1)

ikop
ikop

Reputation: 1790

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

Upvotes: 6

Related Questions