@@ -57,9 +57,7 @@ guide_coloursteps <- function(
5757 guide_colourbar(
5858 even.steps = even.steps ,
5959 show.limits = show.limits ,
60- raster = FALSE ,
6160 ticks = ticks ,
62- nbin = 100 ,
6361 ... ,
6462 super = GuideColoursteps
6563 )
@@ -97,7 +95,7 @@ GuideColoursteps <- ggproto(
9795 breaks <- parsed $ breaks
9896
9997 key <- data_frame(scale $ map(breaks ), .name_repair = ~ aesthetic )
100- key $ .value <- seq_along(breaks ) - 0.5
98+ key $ .value <- seq_along(breaks )
10199 key $ .label <- scale $ get_labels(breaks )
102100
103101 if (breaks [1 ] %in% limits ) {
@@ -117,31 +115,29 @@ GuideColoursteps <- ggproto(
117115 extract_decor = function (scale , aesthetic , key ,
118116 reverse = FALSE , even.steps = TRUE ,
119117 nbin = 100 , ... ) {
120- if (! (even.steps || ! is.numeric(scale $ get_breaks()))) {
121- return (GuideColourbar $ extract_decor(scale , aesthetic , reverse = reverse ,
122- nbin = nbin ))
123- }
124-
125- bin_at <- attr(key , " bin_at" , TRUE )
126-
127- bar <- data_frame0(
128- colour = scale $ map(bin_at ),
129- value = seq_along(bin_at ) - 1 ,
130- .size = length(bin_at )
131- )
132- if (reverse ) {
133- bar <- bar [nrow(bar ): 1 , , drop = FALSE ]
118+ if (even.steps ) {
119+ bin_at <- attr(key , " bin_at" , TRUE )
120+ bar <- data_frame0(
121+ colour = scale $ map(bin_at ),
122+ min = seq_along(bin_at ) - 1 ,
123+ max = seq_along(bin_at ),
124+ .size = length(bin_at )
125+ )
126+ } else {
127+ breaks <- unique(sort(c(scale $ get_limits(), scale $ get_breaks())))
128+ n <- length(breaks )
129+ bin_at <- (breaks [- 1 ] + breaks [- n ]) / 2
130+ bar <- data_frame0(
131+ colour = scale $ map(bin_at ),
132+ min = head(breaks , - 1 ),
133+ max = tail(breaks , - 1 ),
134+ .size = length(bin_at )
135+ )
134136 }
135137 return (bar )
136138 },
137139
138- extract_params = function (scale , params , ... ) {
139-
140- if (params $ even.steps ) {
141- params $ nbin <- nbin <- sum(! is.na(params $ key [[1 ]])) + 1
142- } else {
143- nbin <- params $ nbin
144- }
140+ extract_params = function (scale , params , direction = " vertical" , title = waiver(), ... ) {
145141
146142 show.limits <- params $ show.limits %|| % scale $ show.limits %|| % FALSE
147143
@@ -158,25 +154,56 @@ GuideColoursteps <- ggproto(
158154 }
159155
160156 if (show.limits ) {
161- edges <- rescale(
162- c(0 , 1 ),
163- to = params $ decor $ value [c(1 , nrow(params $ decor ))],
164- from = c(0.5 , nbin - 0.5 ) / nbin
165- )
166157 key <- params $ key
167158 limits <- attr(key , " limits" , TRUE ) %|| % scale $ get_limits()
168159 key <- key [c(NA , seq_len(nrow(key )), NA ), , drop = FALSE ]
169- key $ .value [c(1 , nrow(key ))] <- edges
170- key $ .label [c(1 , nrow(key ))] <- scale $ get_labels(limits )
160+ n <- nrow(key )
161+ key $ .value [c(1 , n )] <- range(params $ decor $ min , params $ decor $ max )
162+ key $ .label [c(1 , n )] <- scale $ get_labels(limits )
171163 if (key $ .value [1 ] == key $ .value [2 ]) {
172- key <- key [- 1 , , drop = FALSE ]
164+ key <- vec_slice(key , - 1 )
165+ n <- n - 1
173166 }
174- if (key $ .value [nrow( key ) - 1 ] == key $ .value [nrow( key ) ]) {
175- key <- key [ - nrow (key ), , drop = FALSE ]
167+ if (key $ .value [n - 1 ] == key $ .value [n ]) {
168+ key <- vec_slice (key , - n )
176169 }
177170 params $ key <- key
178171 }
179172
180- GuideColourbar $ extract_params(scale , params , ... )
173+ params $ title <- scale $ make_title(
174+ params $ title %| W | % scale $ name %| W | % title
175+ )
176+
177+ limits <- c(params $ decor $ min [1 ], params $ decor $ max [nrow(params $ decor )])
178+ if (params $ reverse ) {
179+ limits <- rev(limits )
180+ }
181+ params $ key $ .value <- rescale(params $ key $ .value , from = limits )
182+ params $ decor $ min <- rescale(params $ decor $ min , from = limits )
183+ params $ decor $ max <- rescale(params $ decor $ max , from = limits )
184+ params
185+ },
186+
187+ build_decor = function (decor , grobs , elements , params ) {
188+
189+ size <- abs(decor $ max - decor $ min )
190+ just <- as.numeric(decor $ min > decor $ max )
191+ gp <- gpar(col = NA , fill = decor $ colour )
192+ if (params $ direction == " vertical" ) {
193+ grob <- rectGrob(
194+ x = 0 , y = decor $ min ,
195+ width = 1 , height = size ,
196+ vjust = just , hjust = 0 , gp = gp
197+ )
198+ } else {
199+ grob <- rectGrob(
200+ x = decor $ min , y = 0 ,
201+ height = 1 , width = size ,
202+ hjust = just , vjust = 0 , gp = gp
203+ )
204+ }
205+
206+ frame <- element_grob(elements $ frame , fill = NA )
207+ list (bar = grob , frame = frame , ticks = grobs $ ticks )
181208 }
182209)
0 commit comments