@@ -4,6 +4,7 @@ dotstackGrob <- function(
4
4
stackaxis = " y" ,
5
5
dotdia = unit(1 , " npc" ), # Dot diameter in the non-stack axis, should be in npc
6
6
stackposition = 0 , # Position of each dot in the stack, relative to origin
7
+ stackdir = " up" , # Stacking direction ("up", "down", "center", or "centerwhole")
7
8
stackratio = 1 , # Stacking height of dots (.75 means 25% dot overlap)
8
9
default.units = " npc" , name = NULL , gp = gpar(), vp = NULL )
9
10
{
@@ -17,7 +18,7 @@ dotstackGrob <- function(
17
18
warn(" Unit type of dotdia should be 'npc'" )
18
19
19
20
grob(x = x , y = y , stackaxis = stackaxis , dotdia = dotdia ,
20
- stackposition = stackposition , stackratio = stackratio ,
21
+ stackposition = stackposition , stackdir = stackdir , stackratio = stackratio ,
21
22
name = name , gp = gp , vp = vp , cl = " dotstackGrob" )
22
23
}
23
24
# Only cross-version reliable way to check the unit of a unit object
@@ -31,14 +32,27 @@ makeContext.dotstackGrob <- function(x, recording = TRUE) {
31
32
xmm <- convertX(x $ x , " mm" , valueOnly = TRUE )
32
33
ymm <- convertY(x $ y , " mm" , valueOnly = TRUE )
33
34
35
+ # When stacking up (or down), stackratios != 1 will cause the bottom (top)
36
+ # edge of the first dot in a stack to no longer touch the origin, as
37
+ # stackpositions are expanded or contracted away from the dotstack's origin.
38
+ # The stackoffset corrects that misalignment so that the first dot just
39
+ # touches the dotstack's origin.
40
+ if (is.null(x $ stackdir ) || x $ stackdir == " up" ) {
41
+ stackoffset <- (1 - x $ stackratio ) / 2
42
+ } else if (x $ stackdir == " down" ) {
43
+ stackoffset <- - (1 - x $ stackratio ) / 2
44
+ } else {
45
+ stackoffset <- 0
46
+ }
47
+
34
48
if (x $ stackaxis == " x" ) {
35
49
dotdiamm <- convertY(x $ dotdia , " mm" , valueOnly = TRUE )
36
- xpos <- xmm + dotdiamm * (x $ stackposition * x $ stackratio + ( 1 - x $ stackratio ) / 2 )
50
+ xpos <- xmm + dotdiamm * (x $ stackposition * x $ stackratio + stackoffset )
37
51
ypos <- ymm
38
52
} else if (x $ stackaxis == " y" ) {
39
53
dotdiamm <- convertX(x $ dotdia , " mm" , valueOnly = TRUE )
40
54
xpos <- xmm
41
- ypos <- ymm + dotdiamm * (x $ stackposition * x $ stackratio + ( 1 - x $ stackratio ) / 2 )
55
+ ypos <- ymm + dotdiamm * (x $ stackposition * x $ stackratio + stackoffset )
42
56
}
43
57
44
58
circleGrob(
0 commit comments