r - ggplot2: center legend below plot instead of panel area -
ggplot is the legend beneath the panel from the default center, which is really disappointing in some circumstances See examples given below:
ggplot (diamonds, AES (cut, fill = clarity)) + geom_bar () + coord_flip () + theme (legend.position = 'bottom')
Be seen this The previous label might have emerged from the image, although we have some fine on the left side of the legend The place is - it would be better to use it.
Q : How can I focus the center of the legend under the plot and do not try to force the issue to be centered below the panel area ?
Update : For further examples on this issue:
df < - data.frame (x = sample ('an extremely long labeled label that covers legend placement', letter [1: 7]), 1e3, correct), y = sample (paste ('small label', letter) [1: 7]), 1e3, TRUE) ggplot (df, aes (x, fill = y)) + geom_bar () + coord_flip () + subject (legend.section = 'bottom')
I To do this: To add a legend to the bottom of the panel manually and add some extra margins under the plot I legend .direction :
ggplot (diamonds, ace cut, fill = clarification) + geom_bar () + coord_flip () + theme (legend.position = c (0.37, -0.1), legend.direction = 'horizontal', plot.margin = grid :: unit (C (0.1, 0.12.0.1), 'line')) < Img src = "https://i.stack.imgur.com/h1daC.png" alt = "Enter the image here">
But in this way, "manual" optimum legend Position to calculate i have any suggestions?
Update : I'm arranging several plots next to each other, so I do not want to focus on the actual image, rather a single panel example:
You can edit gtable,
library (gtable) G & lt; - ggplotGrob (p) id & lt; - which (g $ layout $ name == "guide-box") g $ layout [id, c ("l", "r")]
Comments
Post a Comment