问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/42998895/how-to-place-legend-horizontaly-above-barplot-in-r