45
45
# '
46
46
# ' # position of guides
47
47
# '
48
- # ' p + theme(legend.position = "bottom", legend.box = "horizontal")
49
- # '
50
48
# ' # Set order for multiple guides
51
49
# ' ggplot(mpg, aes(displ, cty)) +
52
50
# ' geom_point(aes(size = hwy, colour = cyl, shape = drv)) +
@@ -93,32 +91,24 @@ update_guides <- function(p, guides) {
93
91
# arrange all ggrobs
94
92
95
93
build_guides <- function (scales , layers , default_mapping , position , theme , guides , labels ) {
96
-
97
- # set themes w.r.t. guides
98
- # should these theme$legend.XXX be renamed to theme$guide.XXX ?
99
-
100
- # by default, guide boxes are vertically aligned
101
- theme $ legend.box <- theme $ legend.box %|| % " vertical"
102
-
103
- # size of key (also used for bar in colorbar guide)
104
94
theme $ legend.key.width <- theme $ legend.key.width %|| % theme $ legend.key.size
105
95
theme $ legend.key.height <- theme $ legend.key.height %|| % theme $ legend.key.size
106
96
107
- # by default, direction of each guide depends on the position of the guide.
108
- theme $ legend.direction <-
109
- theme $ legend.direction % || %
110
- if (length( position ) == 1 && position %in% c( " top " , " bottom " , " left " , " right " ))
111
- switch ( position [ 1 ], top = , bottom = " horizontal " , left = , right = " vertical" )
112
- else
113
- " vertical"
114
-
115
- # justification of legend boxes
116
- theme $ legend.box.just <-
117
- theme $ legend.box.just % || %
118
- if (length( position ) == 1 && position %in% c( " top " , " bottom " , " left " , " right " ))
119
- switch ( position , bottom = , top = c( " center " , " top " ), left = , right = c( " left " , " top " ))
120
- else
121
- c( " center " , " center " )
97
+ # Layout of legends depends on their overall location
98
+ position <- legend_position( position )
99
+ if ( position == " inside " ) {
100
+ theme $ legend.box <- theme $ legend.box % || % " vertical "
101
+ theme $ legend.direction <- theme $ legend.direction % || % " vertical"
102
+ theme $ legend.box.just <- theme $ legend.box.just % || % c( " center " , " center " )
103
+ } else if ( position == " vertical" ) {
104
+ theme $ legend.box <- theme $ legend.box % || % " vertical "
105
+ theme $ legend.direction <- theme $ legend.direction % || % " vertical "
106
+ theme $ legend.box.just <- theme $ legend.box.just % || % c( " left " , " top " )
107
+ } else if ( position == " horizontal " ) {
108
+ theme $ legend.box <- theme $ legend.box % || % " horizontal "
109
+ theme $ legend.direction <- theme $ legend.direction % || % " horizontal "
110
+ theme $ legend.box.just <- theme $ legend.box.just % || % c( " center " , " top " )
111
+ }
122
112
123
113
# scales -> data for guides
124
114
gdefs <- guides_train(scales = scales , theme = theme , guides = guides , labels = labels )
@@ -140,6 +130,19 @@ build_guides <- function(scales, layers, default_mapping, position, theme, guide
140
130
grobs
141
131
}
142
132
133
+ # Simplify legend position to one of horizontal/vertical/inside
134
+ legend_position <- function (position ) {
135
+ if (length(position ) == 1 ) {
136
+ if (position %in% c(" top" , " bottom" )) {
137
+ " horizontal"
138
+ } else {
139
+ " vertical"
140
+ }
141
+ } else {
142
+ " inside"
143
+ }
144
+ }
145
+
143
146
# validate guide object
144
147
validate_guide <- function (guide ) {
145
148
# if guide is specified by character, then find the corresponding guide
0 commit comments