Skip to content

Commit 0ceb832

Browse files
authored
Merge pull request #20 from asgr/copilot/fix-github-actions-job
Fix magproj docs/code mismatch and integrate magMWdust from PR #19
2 parents bdba15e + 1759d59 commit 0ceb832

7 files changed

Lines changed: 194 additions & 12 deletions

File tree

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: magicaxis
22
Type: Package
33
Title: Pretty Scientific Plotting with Minor-Tick and Log Minor-Tick Support
4-
Version: 2.6.2
5-
Date: 2026-07-28
4+
Version: 2.7.0
5+
Date: 2026-07-29
66
Authors@R: person(given = "Aaron",
77
family = "Robotham",
88
role = c("aut", "cre"),
@@ -14,3 +14,4 @@ Imports: grDevices, graphics, stats, celestial (>= 1.4.1), MASS, plotrix, sm, ma
1414
Remotes: asgr/ParmOff
1515
VignetteBuilder: knitr
1616
Config/testthat/edition: 3
17+
LazyData: true

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ importFrom("RANN", "nn2")
1414
importFrom("plotrix", "color.legend", "draw.ellipse")
1515
importFrom("sm", "sm.density")
1616
importFrom("MASS", "kde2d")
17-
importFrom("utils", "str")
17+
importFrom("utils", "str", "setTxtProgressBar", "txtProgressBar")
1818
importFrom("ParmOff", "ParmOff")

R/magMWdust.R

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#=========================================
2+
#
3+
# File Name : magMWdust.R
4+
# Created By : awright
5+
# Creation Date : 03-06-2026
6+
# Last Modified : Wed Jun 3 13:40:13 2026
7+
#
8+
#=========================================
9+
10+
utils::globalVariables("SFD_dust")
11+
12+
# Define a plotting helper that overlays Milky Way dust on a projected sky plot.
13+
magMWdust = function(dust.data = NULL, dlon = NULL, dlat = NULL, type = "p", pch = 16, pt.cex = 0.5, opacity.range = c(0, 0.5), whiteblack.percentile = c(0.5, 0.95), stretch = "lin", min.opacity.plot = 0.01, show.status = TRUE, ...) {
14+
15+
# Restrict the drawing style to points or polygons.
16+
if (!type %in% c("pl", "p")) stop("magMWdust function expects type of 'p' (for points) or 'pl' (for polygons) only")
17+
# If none provided, read the dust map data, which is a dlon=dlat=1 sampling
18+
if (is.null(dust.data)) {
19+
# Define the dlon and dlat values
20+
dlon = dlat = 1
21+
# Lazy load the SFD_dust data
22+
dust_all = SFD_dust
23+
} else {
24+
if (!is.data.frame(dust.data)) {
25+
stop("dust.data is not a data frame; load an example with data(SFD_dust)")
26+
}
27+
if (!all(c("ebv","ra","dec")%in%colnames(dust.data))) {
28+
stop("dust.data is missing required components; load an example with data(SFD_dust)")
29+
}
30+
dust_all=dust.data
31+
if (type=='pl') {
32+
if (is.null(dlon)) stop("dlon must be provided when providing input dust.data and using type == 'pl'")
33+
if (is.null(dlat)) stop("dlat must be provided when providing input dust.data and using type == 'pl'")
34+
if (!is.numeric(dlon)) stop("dlon must be numeric")
35+
if (!is.numeric(dlat)) stop("dlat must be numeric")
36+
}
37+
}
38+
# Map dust values onto an opacity scale for plotting.
39+
dust_all$map = magicaxis::magmap(dust_all$ebv, range = opacity.range, hicut = whiteblack.percentile[2], locut = whiteblack.percentile[1], stretch = stretch)$map
40+
# Drop grid cells that would be too faint to plot usefully.
41+
dust = dust_all[which(dust_all$map > min.opacity.plot), ]
42+
# Stop on empty result
43+
if (nrow(dust)==0) stop("threshold for min.opacity.plot causes no data to be plotted. Reduce value to produce a result")
44+
45+
# Draw filled polygons when polygon mode has been requested.
46+
if (type == "pl") {
47+
# Open a progress bar for the per-cell polygon loop.
48+
if (interactive() & isTRUE(show.status)) {
49+
pb = txtProgressBar(style = 3, min = 1, max = nrow(dust))
50+
}
51+
# Iterate over each retained sky cell.
52+
for (i in 1:nrow(dust)) {
53+
# Project and draw the four corners of the current sky cell.
54+
magicaxis::magproj(c(dust$ra[i] - dlon/2, dust$ra[i] - dlon/2, dust$ra[i] + dlon/2, dust$ra[i] + dlon/2),
55+
c(dust$dec[i] - dlat/2, dust$dec[i] + dlat/2, dust$dec[i] + dlat/2, dust$dec[i] - dlat/2),
56+
type = type, add = TRUE, col = hsv(v = 0, alpha = dust$map[i]), border=NA, ...)
57+
# Advance the progress bar after drawing the current polygon.
58+
if (interactive() & isTRUE(show.status)) {
59+
setTxtProgressBar(pb, i)
60+
}
61+
}
62+
# Close the progress bar when the polygon layer is complete.
63+
if (interactive() & isTRUE(show.status)) {
64+
close(pb)
65+
}
66+
} else {
67+
# Draw the retained dust grid cells as projected points.
68+
magicaxis::magproj(dust$ra, dust$dec, type = "p", add = TRUE, pch = pch, cex = pt.cex, col = hsv(v = 0, alpha = dust$map), ...)
69+
}
70+
# Return invisibly because this function is used for its plotting side effects.
71+
return(invisible(NULL))
72+
}

R/magproj.R

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ magproj=function(long, lat, type='b', plottext, longlim=c(-180,180), latlim=c(-9
1515
projection="aitoff", parameters=NULL, centre=c(0,0), add=FALSE,
1616
fliplong=FALSE, nlat=6, nlong=6, prettybase=30, labels=TRUE, grid=TRUE,
1717
grid.col='grey', grid.lty=2, auto=FALSE, upres=100, box=TRUE, labloc=c(90,-45),
18-
labeltype='deg', crunch=FALSE, ...){
18+
labeltype='deg', crunch=FALSE, lab.cex=1, ...){
1919

2020
if(is.matrix(long) | is.data.frame(long)){
2121
lat = long[, 2]
@@ -42,7 +42,14 @@ magproj=function(long, lat, type='b', plottext, longlim=c(-180,180), latlim=c(-9
4242
}
4343

4444
long= (long+(180-orientation[2])) %% 360 - (180-orientation[2])
45-
lat= (lat+90) %% 180 - 90
45+
.lat_wrap <- function(x) {
46+
x[abs(x)==180]<-0
47+
x=(x+180)%%360-180
48+
x[which(x>= +90)]<- +90-x[which(x>= +90)]%%90
49+
x[which(x<= -90)]<- -90+abs(x[which(x<= -90)])%%90
50+
return=x
51+
}
52+
lat= .lat_wrap(lat)
4653

4754
if(add==FALSE){
4855
if(auto==TRUE){
@@ -97,16 +104,16 @@ magproj=function(long, lat, type='b', plottext, longlim=c(-180,180), latlim=c(-9
97104
latpretty=latgrid$tickat
98105
latpretty=latpretty[latpretty>latlim[1] & latpretty<latlim[2]]
99106
temp=mapproject(longpretty, rep(labloc[2],length(longpretty)))
100-
if(labeltype=='deg'){text(temp,labels = longpretty %% 360)}
107+
if(labeltype=='deg'){text(temp,labels = longpretty %% 360,cex = lab.cex)}
101108
if(labeltype=='sex'){
102-
if(crunch==FALSE){text(temp,labels = deg2hms(longpretty %% 360,type='cat'))}
103-
if(crunch==TRUE){text(temp,labels = paste(deg2hms(longpretty %% 360,type='mat')[,1],'h',sep=''))}
109+
if(crunch==FALSE){text(temp,labels = deg2hms(longpretty %% 360,type='cat'),cex = lab.cex)}
110+
if(crunch==TRUE){text(temp,labels = paste(deg2hms(longpretty %% 360,type='mat')[,1],'h',sep=''),cex = lab.cex)}
104111
}
105112
temp=mapproject(rep(labloc[1],length(latpretty)), latpretty)
106-
if(labeltype=='deg'){text(temp,labels = latpretty)}
113+
if(labeltype=='deg'){text(temp,labels = latpretty,cex = lab.cex)}
107114
if(labeltype=='sex'){
108-
if(crunch==FALSE){text(temp,labels = deg2dms(latpretty,type='cat'))}
109-
if(crunch==TRUE){text(temp,labels = paste(deg2dms(latpretty,type='mat')[,1],'\u00B0',sep=''))}
115+
if(crunch==FALSE){text(temp,labels = deg2dms(latpretty,type='cat'),cex = lab.cex)}
116+
if(crunch==TRUE){text(temp,labels = paste(deg2dms(latpretty,type='mat')[,1],'\u00B0',sep=''),cex = lab.cex)}
110117
}
111118
}
112119
}else{

data/SFD_dust.rda

288 KB
Binary file not shown.

man/magMWdust.Rd

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
\name{magMWdust}
2+
\alias{magMWdust}
3+
\alias{SFD_dust}
4+
\title{
5+
Plot a Milky Way dust overlay
6+
}
7+
\description{
8+
Adds a projected Milky Way dust layer to an existing sky plot using a lookup-table
9+
constructed from the SFD dust map on an equatorial grid, and drawing the result as
10+
points or filled polygons.
11+
}
12+
\usage{
13+
magMWdust(dust.data = NULL, dlon = NULL, dlat = NULL, type = "p", pch = 16, pt.cex = 0.5,
14+
opacity.range = c(0, 0.5), whiteblack.percentile = c(0.5, 0.95), stretch = "lin",
15+
min.opacity.plot = 0.01, show.status = TRUE, ...)
16+
}
17+
\arguments{
18+
\item{dust.data}{
19+
The data set to use for drawing the dust map. If \code{NULL} (the default), the function
20+
lazy-loads the SFD dust map data provided with the package (\code{data("SFD_dust")}). The
21+
code expects this data to be a data frame containing columns "ra", "dec", and "ebv".
22+
}
23+
\item{dlon}{
24+
Step size between element of longitude. Required when specifying dust.data directly,
25+
and uses \code{dlon=1} when lazy-loading the internal dust map.
26+
}
27+
\item{dlat}{
28+
Step size between element of latitude. Required when specifying dust.data directly,
29+
and uses \code{dlat=1} when lazy-loading the internal dust map.
30+
}
31+
\item{type}{
32+
Character scalar giving the drawing mode: \code{"p"} for points or \code{"pl"}
33+
for polygons.
34+
}
35+
\item{pch}{
36+
Plotting symbol used when \code{type = "p"}.
37+
}
38+
\item{pt.cex}{
39+
Point expansion factor used when \code{type = "p"}.
40+
}
41+
\item{opacity.range}{
42+
Two-element numeric vector giving the output opacity range passed to
43+
\code{\link{magmap}}.
44+
}
45+
\item{whiteblack.percentile}{
46+
Two-element numeric vector giving the lower and upper percentiles used when
47+
mapping dust values to opacity.
48+
}
49+
\item{stretch}{
50+
Stretch mode passed through to \code{\link{magmap}}.
51+
}
52+
\item{min.opacity.plot}{
53+
Minimum mapped opacity required for a grid cell to be drawn.
54+
}
55+
\item{show.status}{
56+
Option to show a progress bar when drawing polygons.
57+
}
58+
\item{\dots}{
59+
Additional arguments passed to \code{\link{magproj}}.
60+
}
61+
}
62+
\details{
63+
The function plots \code{E(B-V)} values from a provided dust map data.frame, or if none is provided uses a lazy-loaded
64+
SFD dust map which is sampled on a 1x1 deg grid. This lookup table is then mapped to points (or polygons, see below)
65+
with opacity determined by \code{\link{magmap}}, using the requested white/black points defined using percentiles
66+
(\code{whiteblack.percentile}) of the map, and throwing away cells whose opacity is below the requested
67+
minimum (\code{min.opacity.plot}).
68+
69+
When \code{type = "pl"} each retained grid cell is drawn as a projected
70+
four-corner polygon. Otherwise the retained cells are drawn as projected
71+
points. Polygon drawing is very slow when drawing to X11 (many minutes) except
72+
with very low resolutions. Polygon drawing may nonetheless be preferable when
73+
writing to file, where the operation is much faster and reduces aliasing effects
74+
caused by over- (or under-)lapping points.
75+
}
76+
\value{
77+
Returns \code{NULL} invisibly. The function is called for its plotting side
78+
effects.
79+
}
80+
\references{
81+
Schlegel, Finkbeiner, & Davis, 1998, ApJ, 500, 2, 525
82+
}
83+
\author{
84+
Angus H Wright
85+
}
86+
\seealso{
87+
\code{\link{magmap}}, \code{\link{magproj}}
88+
}
89+
\examples{
90+
magproj(0,0,type='n') #Construct a base magproj plot
91+
magMWdust() #add MW dust map as points
92+
93+
#the below will take about a minute to generate
94+
\dontrun{
95+
magproj(0,0,type='n')
96+
magMWdust(type = "pl") #add MW dust map as polygons
97+
}
98+
}
99+
\keyword{plot}

man/magproj.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ magproj(long, lat, type = "b", plottext, longlim = c(-180, 180), latlim = c(-90,
1313
projection = "aitoff", parameters = NULL, centre = c(0, 0), add = FALSE,
1414
fliplong = FALSE, nlat = 6, nlong = 6, prettybase = 30, labels = TRUE, grid = TRUE,
1515
grid.col = "grey", grid.lty = 2, auto = FALSE, upres = 100, box = TRUE,
16-
labloc = c(90, -45), labeltype = "deg", crunch = FALSE, ...)
16+
labloc = c(90, -45), labeltype = "deg", crunch = FALSE, lab.cex = 1, ...)
1717

1818
magprojgrid(nlat = 6, nlong = 6, prettybase = 30, box = TRUE, ...)
1919

@@ -93,6 +93,9 @@ Should the labels be drawn using degrees (deg) or colon delimited sexigesimal (s
9393
}
9494
\item{crunch}{
9595
If set to FALSE the full output of \code{\link[celestial]{deg2hms}} and \code{\link[celestial]{deg2dms}} is printed. If set to TRUE a simplified output is used, where only the hours and degrees parts are extracted and appended with a 'h' and a degree symbol respectively.
96+
}
97+
\item{lab.cex}{
98+
Character expansion factor for the coordinate labels. Default is 1. Passed as \option{cex} to the internal \code{\link{text}} calls that draw the longitude and latitude labels.
9699
}
97100
\item{\dots}{
98101
For magproj, Extra options that are either passed to \code{\link{points}} (\option{type='p'}), \code{\link{lines}} (\option{type='l'}), \code{\link{polygon}} (\option{type='pl'}), \code{\link{text}} (\option{type='t'}), or \code{\link{polygon}} (\option{type='b'}). For magprojgrid dots are pased to \code{\link{lines}} for drawing the grid lines. For magprojlabels dots are passed to \code{\link{text}} for adding text labels.

0 commit comments

Comments
 (0)