how to place legend horizontaly above barplot in r

霸气de小男生 提交于 2021-02-16 14:37:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!