|
| 1 | +# grayplot |
| 2 | +# |
| 3 | +# Scatterplot with a gray background |
| 4 | +# |
| 5 | +# Like the plot function, but using a gray background just |
| 6 | +# for the plot region. |
| 7 | +# |
| 8 | +# @param x Coordinates of points in the plot |
| 9 | +# |
| 10 | +# @param y Coordinates of points in the plot (optional) |
| 11 | +# |
| 12 | +# @param ... Optional graphics arguments |
| 13 | +# |
| 14 | +# @param type Plot type (points, lines, etc.) |
| 15 | +# |
| 16 | +# @param hlines Locations of horizontal grid lines; use `hlines=NA` to prevent horizontal grid lines |
| 17 | +# |
| 18 | +# @param hlines.col Colors of horizontal grid lines |
| 19 | +# |
| 20 | +# @param hlines.lty Line type of horizontal grid lines |
| 21 | +# |
| 22 | +# @param hlines.lwd Line width of horizontal grid lines |
| 23 | +# |
| 24 | +# @param vlines Locations of vertical grid lines; use `vlines=NA` to prevent vertical grid lines |
| 25 | +# |
| 26 | +# @param vlines.col Colors of vertical grid lines |
| 27 | +# |
| 28 | +# @param vlines.lty Line type of vertical grid lines |
| 29 | +# |
| 30 | +# @param vlines.lwd Line width of vertical grid lines |
| 31 | +# |
| 32 | +# @param xat Locations for x-axis labels; `xat=NA` indicates no labels |
| 33 | +# |
| 34 | +# @param yat Locations for y-axis labels; `yat=NA` indicates no labels |
| 35 | +# |
| 36 | +# @param bgcolor Background color |
| 37 | +# |
| 38 | +# @param pch point type |
| 39 | +# @param bg Background color in points |
| 40 | +# @param col Color of outer circle in points |
| 41 | +# |
| 42 | +# @param v_over_h If `TRUE`, place vertical grid lines on top of |
| 43 | +# the horizontal ones. |
| 44 | +# |
| 45 | +# @details |
| 46 | +# Calls `plot()` with `type="n"`, then [graphics::rect()] to |
| 47 | +# get the background, and then [graphics::points()]. Additional |
| 48 | +# arguments you can include: `mgp.x` and `mgp.y` (like `mgp`, for |
| 49 | +# controlling parameters of axis labels, but separate for x- and |
| 50 | +# y-axis). |
| 51 | +# |
| 52 | +# @importFrom graphics title rect axis abline points |
| 53 | +# |
| 54 | +# @return |
| 55 | +# None. |
| 56 | +# |
| 57 | +# @examples |
| 58 | +# \dontshow{set.seed(97536917)} |
| 59 | +# x <- rnorm(100) |
| 60 | +# y <- x+rnorm(100, 0, 0.7) |
| 61 | +# grayplot(x, y, col="slateblue", pch=16) |
| 62 | +# at <- seq(-3, 3) |
| 63 | +# grayplot(x, y, col="violetred", pch=16, hlines=at, vlines=at) |
| 64 | +# grayplot(x, col="Orchid", pch=16, bgcolor="gray80", |
| 65 | +# hlines=seq(-4, 4, by=0.5), hlines.lwd=c(3,1), |
| 66 | +# vlines=seq(0, 100, by=5), vlines.lwd=c(3,1,1,1)) |
| 67 | +# |
| 68 | +# @seealso |
| 69 | +# [dotplot()], [timeplot()], [graphics::par()], [graphics::rect()], [graphics::points()] |
| 70 | +# |
| 71 | +# @keywords |
| 72 | +# graphics |
| 73 | +qtl2_grayplot <- |
| 74 | + function(x, y=NULL, ..., type="p", hlines=NULL, hlines.col="white", hlines.lty=1, hlines.lwd=1, |
| 75 | + vlines=NULL, vlines.col="white", vlines.lty=1, vlines.lwd=1, |
| 76 | + xat=NULL, yat=NULL, bgcolor="gray90", |
| 77 | + pch=21, bg="lightblue", col="black", |
| 78 | + v_over_h=FALSE) |
| 79 | +{ |
| 80 | + if(missing(x) || is.null(x)) stop("x unspecified") |
| 81 | + |
| 82 | + # this is to deal with varying inputs (did "..." include xaxt or not?) |
| 83 | + hidegrayplot <- |
| 84 | + function(x, y, ..., type="p", hlines=NULL, hlines.col, hlines.lty, hlines.lwd, |
| 85 | + vlines=NULL, vlines.col, vlines.lty, vlines.lwd, |
| 86 | + xat=pretty(x), yat=pretty(y), bgcolor="gray90", xaxt="n", yaxt="n", |
| 87 | + col.lab=par("col.lab"), |
| 88 | + xlim=NULL, ylim=NULL, |
| 89 | + xlab, ylab, xname, yname, |
| 90 | + las=1, mgp=c(2.1, 0.5, 0), mgp.x=NULL, mgp.y=NULL, |
| 91 | + pch=21, bg="lightblue", col="black", |
| 92 | + v_over_h=FALSE) |
| 93 | + { |
| 94 | + if(is.null(mgp.x)) mgp.x <- mgp |
| 95 | + if(is.null(mgp.y)) mgp.y <- mgp |
| 96 | + |
| 97 | + if(is.null(y)) { |
| 98 | + if(missing(xlab)) xlab <- "Index" |
| 99 | + if(missing(ylab)) ylab <- xname |
| 100 | + y <- x |
| 101 | + x <- seq(along=x) |
| 102 | + } |
| 103 | + else { |
| 104 | + if(missing(xlab)) xlab <- xname |
| 105 | + if(missing(ylab)) ylab <- yname |
| 106 | + } |
| 107 | + |
| 108 | + if(is.null(ylim)) |
| 109 | + ylim <- range(y, na.rm=TRUE) |
| 110 | + if(is.null(hlines)) { |
| 111 | + if(!is.null(yat)) |
| 112 | + hlines <- yat |
| 113 | + else |
| 114 | + hlines <- pretty(ylim) |
| 115 | + } |
| 116 | + else if(length(hlines)==1 && is.na(hlines)) |
| 117 | + hlines <- NULL |
| 118 | + |
| 119 | + if(is.null(xlim)) |
| 120 | + xlim <- range(x, na.rm=TRUE) |
| 121 | + if(is.null(vlines)) { |
| 122 | + if(!is.null(xat)) |
| 123 | + vlines <- xat |
| 124 | + else |
| 125 | + vlines <- pretty(xlim) |
| 126 | + } |
| 127 | + else if(length(vlines)==1 && is.na(vlines)) |
| 128 | + vlines <- NULL |
| 129 | + |
| 130 | + # blank plot |
| 131 | + plot(x, y, ..., type="n", xaxt="n", yaxt="n", xlab="", ylab="", |
| 132 | + xlim=xlim, ylim=ylim) |
| 133 | + |
| 134 | + # axis titles |
| 135 | + graphics::title(xlab=xlab, mgp=mgp.x, col.lab=col.lab) |
| 136 | + graphics::title(ylab=ylab, mgp=mgp.y, col.lab=col.lab) |
| 137 | + |
| 138 | + # add gray rectangle |
| 139 | + u <- par("usr") |
| 140 | + graphics::rect(u[1], u[3], u[2], u[4], col=bgcolor, border="black") |
| 141 | + |
| 142 | + # x axis: if adding white lines, skip the tick marks and move the numbers closer |
| 143 | + if(!(!is.null(xat) && length(xat)==1 && is.na(xat))) { # if a single NA, skip x-axis |
| 144 | + if(!is.null(xat)) { |
| 145 | + if(!is.null(vlines)) |
| 146 | + graphics::axis(side=1, at=xat, mgp=mgp.x, tick=FALSE, las=las) |
| 147 | + else |
| 148 | + graphics::axis(side=1, at=xat, las=las) |
| 149 | + } |
| 150 | + else { |
| 151 | + if(!is.null(vlines)) |
| 152 | + graphics::axis(side=1, mgp=mgp.x, tick=FALSE, las=las) |
| 153 | + else |
| 154 | + graphics::axis(side=1, las=las) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + # y axis: like the x-axis |
| 159 | + if(!(!is.null(yat) && length(yat)==1 && is.na(yat))) { # if a single NA, skip y-axis |
| 160 | + if(!is.null(yat)) { |
| 161 | + if(!is.null(hlines)) |
| 162 | + graphics::axis(side=2, at=yat, mgp=mgp.y, tick=FALSE, las=las) |
| 163 | + else |
| 164 | + graphics::axis(side=2, at=yat, las=las) |
| 165 | + } |
| 166 | + else { |
| 167 | + if(!is.null(hlines)) |
| 168 | + graphics::axis(side=2, mgp=mgp.y, tick=FALSE, las=las) |
| 169 | + else |
| 170 | + graphics::axis(side=2, las=las) |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if(!is.null(vlines) && !v_over_h) |
| 175 | + graphics::abline(v=vlines, col=vlines.col, lty=vlines.lty, lwd=vlines.lwd) |
| 176 | + if(!is.null(hlines)) |
| 177 | + graphics::abline(h=hlines, col=hlines.col, lty=hlines.lty, lwd=hlines.lwd) |
| 178 | + if(!is.null(vlines) && v_over_h) |
| 179 | + graphics::abline(v=vlines, col=vlines.col, lty=vlines.lty, lwd=vlines.lwd) |
| 180 | + |
| 181 | + graphics::points(x, y, ..., pch=pch, bg=bg, col=col, type=type) |
| 182 | + |
| 183 | + # add black border again |
| 184 | + graphics::abline(v=u[1:2], h=u[3:4]) |
| 185 | + } |
| 186 | + |
| 187 | + hidegrayplot(x=x, y=y, ..., type=type, hlines=hlines, hlines.col=hlines.col, |
| 188 | + hlines.lty=hlines.lty, hlines.lwd=hlines.lwd, |
| 189 | + vlines=vlines, vlines.col=vlines.col, |
| 190 | + vlines.lty=vlines.lty, vlines.lwd=vlines.lwd, |
| 191 | + xat=xat, yat=yat, bgcolor=bgcolor, |
| 192 | + pch=pch, bg=bg, col=col, |
| 193 | + xname=substitute(x), yname=substitute(y), |
| 194 | + v_over_h=v_over_h) |
| 195 | + invisible() |
| 196 | +} |
0 commit comments