Skip to content

Commit 9ff7f36

Browse files
committed
Merge pull request #347 from jread-usgs/axis_sides
Moving axis calls to sides
2 parents 185542a + 5519cfb commit 9ff7f36

20 files changed

+107
-105
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: gsplot
22
Type: Package
33
Title: Geological Survey Plotting
4-
Version: 0.6.0
5-
Date: 2016-04-09
4+
Version: 0.6.1
5+
Date: 2016-05-12
66
Authors@R: c( person("Jordan", "Read", role = "aut",
77
email = "[email protected]"),
88
person("Laura", "DeCicco", role = c("aut","cre"),

R/access-gsplot.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,14 @@ lim <- function(object, side=NULL, axis = NULL, set.undefined=TRUE, if.null=c(0,
113113

114114
}
115115

116-
lims <- lapply(side.names, function(x) object[[x]]$lim) %>%
117-
setNames(side.names)
116+
lims <- lapply(side.names, function(x) {
117+
lim <- object[[x]]$lim
118+
if (object[[x]]$reverse){
119+
lim <- rev(lim)
120+
}
121+
return(lim)
122+
})
123+
names(lims) <- side.names
118124
if (!is.null(side) && length(side)==1){
119125
lims <- lims[[1]]
120126
if (set.undefined && all(is.na(lims))){

R/axis.R

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -71,79 +71,79 @@ axis.gsplot <- function(object, ..., n.minor=0, tcl.minor=0.15, reverse=NULL) {
7171

7272
sides <- user_args[[fun.name]]$side
7373
user_args[[fun.name]]$side <- NULL
74+
user_args[[fun.name]]$n.minor <- n.minor
75+
user_args[[fun.name]]$tcl.minor <- tcl.minor
7476

75-
for(i in sides){
76-
arguments1 <- list(arguments = append(list(side=i), user_args[[fun.name]]),
77-
gs.config=list(n.minor=n.minor, tcl.minor=tcl.minor,
78-
reverse=reverse))
79-
to.gsplot <- list(arguments1)
80-
names(to.gsplot) <- fun.name
77+
for(side in sides){
78+
# append the side and give it defaults if it doesn't exist
79+
object <- modify_side(object, args = list(), side=side)
8180

82-
object <- append(object, to.gsplot)
81+
object[[as.side_name(side)]][['axis']] <- append_replace(object[[as.side_name(side)]][['axis']], user_args[[fun.name]])
82+
if (!is.null(reverse)){
83+
object[[as.side_name(side)]][['reverse']] <- reverse
84+
}
8385
}
8486
class(object) <- 'gsplot'
8587
return(object)
8688

8789
}
8890

89-
draw_axis <- function(gsplot, index.axis) {
90-
91-
draw_axis_execute <- function(axisParams, n.minor, index){
91+
draw_axis <- function(axis.args){
92+
93+
# need a cleaner way to extract the non-axis args (such as n.minor and tcl.minor)
94+
95+
if(!exists('n.minor',axis.args) || axis.args$n.minor == 0){
96+
axis.args$n.minor <- NULL
97+
axis.args$tcl.minor <- NULL
98+
axis(axis.args)
99+
} else {
100+
n.minor <- axis.args$n.minor + 1
92101

93-
if(n.minor == 0){
94-
axis(axisParams)
95-
} else {
96-
axis(axisParams)
97-
n.minor <- n.minor + 1
98-
99-
# Minor axis:
100-
101-
#if user hasn't specified "at", calculate it
102-
if(is.null(axisParams$at)){
103-
at <- axTicks(axisParams$side)
104-
} else {
105-
at <- axisParams$at
106-
}
102+
tcl <- NULL
103+
if (exists('tcl.minor',axis.args)){
104+
tcl <- axis.args$tcl.minor
107105

108-
newAT <- c()
109-
110-
if(axisParams$side %% 2 == 0){
111-
logScale <- par()$ylog
112-
} else {
113-
logScale <- par()$xlog
114-
}
115-
116-
if(logScale){
117-
spacing <- median(diff(log10(at)))
118-
at <- c(10^(log10(at[1])-spacing),at,10^(log10(at[length(at)])+spacing))
119-
} else {
120-
spacing <- median(diff(at))
121-
at <- c(at[1]-spacing,at,at[length(at)]+spacing)
122-
}
123-
124-
for(i in 2:length(at)){
125-
newAT <- c(newAT, seq(at[i-1], length.out = n.minor, by= (at[i]-at[i-1])/n.minor))
126-
}
127-
128-
axisParams$at <- newAT
129-
axisParams$tcl <- gsplot[[index]][['gs.config']]$tcl.minor
130-
axisParams$labels <- FALSE
131-
axis(axisParams)
132106
}
133-
}
134-
135-
if(length(index.axis) == 1){
136-
axisParams <- gsplot[[index.axis]][['arguments']]
137-
n.minor <- gsplot[[index.axis]][['gs.config']]$n.minor
138-
draw_axis_execute(axisParams, n.minor, index.axis)
139-
} else {
140107

141-
for(i in index.axis){
142-
axisParams <- gsplot[[i]][['arguments']]
143-
n.minor <- gsplot[[i]][['gs.config']]$n.minor
144-
draw_axis_execute(axisParams, n.minor, i)
108+
axis.args$n.minor <- NULL
109+
axis.args$tcl.minor <- NULL
110+
111+
axis(axis.args)
112+
113+
114+
115+
# Minor axis:
116+
117+
#if user hasn't specified "at", calculate it
118+
if(is.null(axis.args$at)){
119+
at <- axTicks(axis.args$side)
120+
} else {
121+
at <- axis.args$at
122+
}
123+
124+
newAT <- c()
125+
126+
if(axis.args$side %% 2 == 0){
127+
logScale <- par()$ylog
128+
} else {
129+
logScale <- par()$xlog
145130
}
146131

132+
if(logScale){
133+
spacing <- median(diff(log10(at)))
134+
at <- c(10^(log10(at[1])-spacing),at,10^(log10(at[length(at)])+spacing))
135+
} else {
136+
spacing <- median(diff(at))
137+
at <- c(at[1]-spacing,at,at[length(at)]+spacing)
138+
}
139+
140+
for(i in 2:length(at)){
141+
newAT <- c(newAT, seq(at[i-1], length.out = n.minor, by= (at[i]-at[i-1])/n.minor))
142+
}
143+
144+
axis.args$at <- newAT
145+
axis.args$labels <- FALSE
146+
axis.args$tcl <- tcl
147+
axis(axis.args)
147148
}
148-
149-
}
149+
}

R/print.R

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ print.gsplot <- function(x, ...){
4242
}
4343

4444
par(x$global$par)
45-
i.axis <- which(names(views) %in% 'axis')
46-
defined.sides <- sapply(i.axis, function(x) views[[x]][['arguments']][['side']])
4745

4846
bg.arg <- views$bgCol
4947
title.arg <- views$title
@@ -55,14 +53,10 @@ print.gsplot <- function(x, ...){
5553
side <- as.side(side.name)
5654
old.par <- par(x[[side.name]]$par)
5755
set_frame(views, side)
58-
if(!side %in% defined.sides){
59-
if(x[[side.name]][['axes']]){
60-
do.call('Axis', append(list(side=side,x=lim(views, side)), config('axis')))
61-
}
62-
} else {
63-
axis <- i.axis[which(defined.sides == side)]
64-
draw_axis(views, index.axis=axis)
56+
if(x[[side.name]][['axes']]){
57+
draw_axis(x[[side.name]][['axis']])
6558
}
59+
6660
if(par('ann')){
6761
mtext(text=label(views, side), side=side, line = 2, las=config("mtext")$las)
6862
}
@@ -89,8 +83,8 @@ print.gsplot <- function(x, ...){
8983
par(new=TRUE)
9084
}
9185

92-
i.axis.noview <- i.axis[which(!defined.sides %in% c(view.info$x, view.info$y))]
93-
draw_axis(views, index.axis=i.axis.noview)
86+
#i.axis.noview <- i.axis[which(!defined.sides %in% c(view.info$x, view.info$y))]
87+
#draw_axis(views, index.axis=i.axis.noview)
9488

9589
draw_legend(views)
9690
par(new=FALSE)

R/utils-side.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ add_new_side <- function(object, side.name){
222222
stopifnot(length(side.name) == 1)
223223
if (side.name %in% side_names(object))
224224
stop(side.name, ' already exists, cannot add it.', call. = FALSE)
225-
side.template <- list(list(lim = c(NA, NA), log=FALSE, label="", axes = TRUE, reverse = FALSE, usr.lim=c(FALSE, FALSE)))
225+
side.template <- list(list(
226+
axis = set_args('axis', side=as.side(side.name), package='graphics'),
227+
lim = c(NA, NA), log=FALSE, label="", axes = TRUE, reverse = FALSE, usr.lim=c(FALSE, FALSE)))
228+
226229
names(side.template) <- side.name
227230

228231
last.side.i <- max(which_sides(object), 0)

README.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ gs <- gsplot() %>%
260260
ylab="logged y axis", xlab="logged x axis", log='xy') %>%
261261
callouts(x=8, y=4, lwd=2, angle=45, labels="Not sure about this one") %>%
262262
title("logged axes") %>%
263-
axis(side=c(1,2,3,4), labels=FALSE, n.minor=4) %>%
264-
axis(side=c(1,2))
263+
axis(side=c(3,4), labels=FALSE, n.minor=4) %>%
264+
axis(side=c(1,2), n.minor=4)
265265
gs
266266
```
267267

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ myplot <- myplot %>% points(4,3,col="blue")
9494
myplot
9595
```
9696

97-
![](README_files/figure-markdown_github/unnamed-chunk-3-1.png)<!-- -->
97+
![](README_files/figure-markdown_github/unnamed-chunk-3-1.png)
9898

9999
<a name="legend"></a>
100100

@@ -128,7 +128,7 @@ myplot <- gsplot() %>%
128128
myplot
129129
```
130130

131-
![](README_files/figure-markdown_github/unnamed-chunk-5-1.png)<!-- -->
131+
![](README_files/figure-markdown_github/unnamed-chunk-5-1.png)
132132

133133
<a name="limits"></a>
134134

@@ -153,7 +153,7 @@ limitsplot <- gsplot() %>%
153153
limitsplot
154154
```
155155

156-
![](README_files/figure-markdown_github/unnamed-chunk-7-1.png)<!-- -->
156+
![](README_files/figure-markdown_github/unnamed-chunk-7-1.png)
157157

158158
<a name="error"></a>
159159

@@ -179,7 +179,7 @@ errorbarplot <- gsplot() %>%
179179
errorbarplot
180180
```
181181

182-
![](README_files/figure-markdown_github/unnamed-chunk-9-1.png)<!-- -->
182+
![](README_files/figure-markdown_github/unnamed-chunk-9-1.png)
183183

184184
<a name="callouts"></a>
185185

@@ -204,7 +204,7 @@ calloutsplot <- gsplot() %>%
204204
calloutsplot
205205
```
206206

207-
![](README_files/figure-markdown_github/unnamed-chunk-11-1.png)<!-- -->
207+
![](README_files/figure-markdown_github/unnamed-chunk-11-1.png)
208208

209209
<a name="reverse"></a>
210210

@@ -226,7 +226,7 @@ reverseplot <- gsplot() %>%
226226
reverseplot
227227
```
228228

229-
![](README_files/figure-markdown_github/unnamed-chunk-13-1.png)<!-- -->
229+
![](README_files/figure-markdown_github/unnamed-chunk-13-1.png)
230230

231231
<a name="embed"></a>
232232

@@ -256,7 +256,7 @@ embedplot <- gsplot() %>%
256256
embedplot
257257
```
258258

259-
![](README_files/figure-markdown_github/unnamed-chunk-15-1.png)<!-- -->
259+
![](README_files/figure-markdown_github/unnamed-chunk-15-1.png)
260260

261261
<a name="base"></a>
262262

@@ -272,7 +272,7 @@ gs
272272
points(1.2,2.5)
273273
```
274274

275-
![](README_files/figure-markdown_github/unnamed-chunk-16-1.png)<!-- -->
275+
![](README_files/figure-markdown_github/unnamed-chunk-16-1.png)
276276

277277
Improved workflow examples
278278
--------------------------
@@ -295,7 +295,7 @@ demoPlot <- gsplot() %>%
295295
demoPlot
296296
```
297297

298-
![](README_files/figure-markdown_github/unnamed-chunk-17-1.png)<!-- -->
298+
![](README_files/figure-markdown_github/unnamed-chunk-17-1.png)
299299

300300
``` r
301301
gs <- gsplot() %>%
@@ -305,12 +305,12 @@ gs <- gsplot() %>%
305305
ylab="logged y axis", xlab="logged x axis", log='xy') %>%
306306
callouts(x=8, y=4, lwd=2, angle=45, labels="Not sure about this one") %>%
307307
title("logged axes") %>%
308-
axis(side=c(1,2,3,4), labels=FALSE, n.minor=4) %>%
309-
axis(side=c(1,2))
308+
axis(side=c(3,4), labels=FALSE, n.minor=4) %>%
309+
axis(side=c(1,2), n.minor=4)
310310
gs
311311
```
312312

313-
![](README_files/figure-markdown_github/unnamed-chunk-18-1.png)<!-- -->
313+
![](README_files/figure-markdown_github/unnamed-chunk-18-1.png)
314314

315315
``` r
316316
usrDef <- gsplot(mar=c(4,4,4,4), xaxs='r', yaxs='r') %>%
@@ -322,7 +322,7 @@ usrDef <- gsplot(mar=c(4,4,4,4), xaxs='r', yaxs='r') %>%
322322
usrDef
323323
```
324324

325-
![](README_files/figure-markdown_github/unnamed-chunk-19-1.png)<!-- -->
325+
![](README_files/figure-markdown_github/unnamed-chunk-19-1.png)
326326

327327
Disclaimer
328328
----------
39 Bytes
Loading
-37 Bytes
Loading
127 Bytes
Loading

0 commit comments

Comments
 (0)