1010# ' @param ratio.colbar The width ratio of colorbar to the total colorlegend
1111# ' (including colorbar, segments and labels).
1212# ' @param lim.segment Vector (quantile) of length 2, the elements should be in
13- # ' [-1 ,1], giving segments coordinates ranges.
13+ # ' [0 ,1], giving segments coordinates ranges.
1414# ' @param align Character, alignment type of labels, \code{"l"} means left,
1515# ' \code{"c"} means center and \code{"r"} right.
1616# ' @param addlabels Logical, whether add text label or not.
2020# ' @keywords hplot
2121# ' @author Taiyun Wei
2222# ' @export
23- colorlegend <- function (colbar , labels , at = NULL ,
24- xlim = c(0 , 1 ), ylim = c(0 , 1 ), vertical = TRUE , ratio.colbar = 0.4 ,
25- lim.segment = NULL , align = c(" c" , " l" , " r" ), addlabels = TRUE ,
26- ... ) {
27-
28- if (is.null(at ) & addlabels ) {
23+ colorlegend <- function (
24+ colbar ,
25+ labels ,
26+ at = NULL ,
27+ xlim = c(0 , 1 ),
28+ ylim = c(0 , 1 ),
29+ vertical = TRUE ,
30+ ratio.colbar = 0.4 ,
31+ lim.segment = NULL ,
32+ align = c(" c" , " l" , " r" ),
33+ addlabels = TRUE ,
34+ ... )
35+ {
36+ if (is.null(at ) && addlabels ) {
2937 at <- seq(0L , 1L , length = length(labels ))
3038 }
3139
3240 if (is.null(lim.segment )) {
33- lim.segment <- ratio.colbar + c(0 , ratio.colbar / 5 )
41+ lim.segment <- ratio.colbar + c(0 , ratio.colbar * .2 )
3442 }
3543
36- if (any(at < 0L ) | any(at > 1L )) {
44+ if (any(at < 0L ) || any(at > 1L )) {
3745 stop(" at should be between 0 and 1" )
3846 }
3947
40- if (any(lim.segment < 0L ) | any(lim.segment > 1L )) {
48+ if (length(lim.segment ) != 2 ) {
49+ stop(" lim.segment should be a vector of length 2" )
50+ }
51+
52+ if (any(lim.segment < 0L ) || any(lim.segment > 1L )) {
4153 stop(" lim.segment should be between 0 and 1" )
4254 }
4355
@@ -57,30 +69,30 @@ colorlegend <- function(colbar, labels, at = NULL,
5769 rep(xlim [1 ] + xgap * rat1 , len ), yyy [- 1 ],
5870 col = colbar , border = colbar )
5971 rect(xlim [1 ], ylim [1 ], xlim [1 ] + xgap * rat1 , ylim [2 ], border = " black" )
60-
61- pos.xlabel <- rep(xlim [1 ] + xgap * max(rat2 , rat1 ), length(at ))
6272 segments(xlim [1 ] + xgap * rat2 [1 ], at , xlim [1 ] + xgap * rat2 [2 ], at )
6373
6474 if (addlabels ) {
75+ pos.xlabel <- rep(xlim [1 ] + xgap * max(rat2 , rat1 ), length(at ))
6576 switch (align ,
6677 l = text(pos.xlabel , y = at , labels = labels , pos = 4 , ... ),
6778 r = text(xlim [2 ], y = at , labels = labels , pos = 2 , ... ),
6879 c = text((pos.xlabel + xlim [2 ]) / 2 , y = at , labels = labels , ... ),
6980 stop(" programming error - should not have reached this line!" )
7081 )
7182 }
72- }
83+ } else {
7384
74- if (! vertical ) {
7585 at <- at * xgap + xlim [1 ]
7686 xxx <- seq(xlim [1 ], xlim [2 ], length = len + 1 )
87+
7788 rect(xxx [1 : len ], rep(ylim [2 ] - rat1 * ygap , len ),
78- xxx [- 1 ], rep(ylim [2 ], len ), col = colbar , border = colbar )
89+ xxx [- 1 ], rep(ylim [2 ], len ),
90+ col = colbar , border = colbar )
7991 rect(xlim [1 ], ylim [2 ] - rat1 * ygap , xlim [2 ], ylim [2 ], border = " black" )
80- pos.ylabel <- rep(ylim [2 ] - ygap * max(rat2 , rat1 ), length(at ))
8192 segments(at , ylim [2 ] - ygap * rat2 [1 ], at , ylim [2 ] - ygap * rat2 [2 ])
8293
8394 if (addlabels ) {
95+ pos.ylabel <- rep(ylim [2 ] - ygap * max(rat2 , rat1 ), length(at ))
8496 switch (align ,
8597 l = text(x = at , y = pos.ylabel , labels = labels , pos = 1 , ... ),
8698 r = text(x = at , y = ylim [1 ], labels = labels , pos = 2 , ... ),
0 commit comments