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')  

Enter image details here

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')  

Enter image details here


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:

+ Coord_flip () + theme (legend.position = 'bottom') pushViewport (Viewpoint (layout = grid layout (nero = 1, niacal = 2, width = unit (c (1, 2), c ("empty" "zero")))) print (p1, vp = viewport ( Layout.pos.row = 1, layout.pos.col = 1)) print (p2, vp = viewport (layout.pos.row = 1, layout.pos.col = 2)) <
 

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

Popular posts from this blog

Python Equivalent for matlab cart2pol and pol2cart -

Docker - Tomcat and PostgreSQL containers in same host - No Route to host -

java - Joda Time Interval Not returning what I expect -