Skip to content

Commit 1536460

Browse files
author
Jordan S Read
committed
Merge pull request #287 from lindsaycarr/master
mtext fixes (WIP)
2 parents d1c5eca + 378cf6a commit 1536460

File tree

4 files changed

+140
-4
lines changed

4 files changed

+140
-4
lines changed

R/mtext.R

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ mtext <- function(object, ...) {
2828
}
2929

3030

31-
mtext.gsplot <- function(object, ..., legend.name=NULL, side=c(1,2)){
32-
set_window_args(object, fun.name='mtext', ..., legend.name=legend.name, side=side, def.funs=graphics::mtext)
33-
}
31+
mtext.gsplot <- function(object, ..., legend.name=NULL, side = 3){
32+
33+
stopifnot(length(side) == 1)
34+
35+
fun.name <- "mtext"
36+
def.funs <- graphics::mtext
37+
38+
user_args <- function_args(name=fun.name, package="graphics", side=side, ...)
39+
40+
to.gsplot <- list(list(arguments = c(do.call(set_args, c(fun.name, user_args)), axes=FALSE),
41+
gs.config = list(legend.name = legend.name, side = side,
42+
par=par_arguments(user_args, def.funs)))) %>%
43+
setNames(fun.name)
44+
45+
object <- append(object, to.gsplot)
46+
47+
return(gsplot(object))
48+
49+
# set_window_args(object, fun.name='mtext', ..., legend.name=legend.name, def.funs=graphics::mtext)
50+
}

inst/extdata/default.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ tcl: 0.3
44
mgp: [1.5, 0.3, 0.0]
55
yaxt: "s"
66
xaxt: "s"
7-
las: 1
87
points:
98
pch: 6
109
col: "red"

tests/testthat/test-mtext.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
context("mtext")
2+
3+
test_that("mtext on correct side", {
4+
gs <- gsplot() %>%
5+
points(1,2) %>%
6+
mtext("my margin text", side=4)
7+
8+
i <- which(unlist(lapply(gs, function(x) {any(names(x) %in% "mtext")})))
9+
10+
expect_true(any(gs[[i]][['window']][['side']] %in% 4))
11+
expect_false(gs[[i]][['window']][['axes']])
12+
})
13+
14+
15+
16+
test_that("multiple mtext are on correct sides", {
17+
gs <- gsplot() %>%
18+
points(1,2) %>%
19+
mtext(text=c(1,2,3,4), at=c(0.7,0.9,1.1,1.3), cex=0.5, las=2, side=1, line=1) %>%
20+
mtext(text=c("yr1", "yr2"), at=c(0.8, 1.2), las=1, side=3, line=3)
21+
22+
expect_true(any(gs[[1]][['window']][['side']] %in% 1))
23+
expect_true(any(names(gs[[1]]) %in% "mtext"))
24+
25+
expect_true(any(gs[[2]][['window']][['side']] %in% 3))
26+
expect_true(any(names(gs[[2]]) %in% "mtext"))
27+
})

vignettes/gsplotIntro.R

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
## ----message=FALSE, echo=TRUE, fig.cap="Demo workflow", fig.width=6, fig.height=6----
2+
library(gsplot)
3+
4+
MaumeeDV <- MaumeeDV
5+
6+
demoPlot <- gsplot() %>%
7+
points(y=c(3,1,2), x=1:3, xlim=c(0,NA),ylim=c(0,NA),
8+
col="blue", pch=18, legend.name="Points", xlab="Index") %>%
9+
lines(c(3,4,3), c(2,4,6), legend.name="Lines", ylab="Data") %>%
10+
abline(b=1, a=0, legend.name="1:1") %>%
11+
legend(location="topleft",title="Awesome!") %>%
12+
grid() %>%
13+
error_bar(x=1:3, y=c(3,1,2), y.high=c(0.5,0.25,1), y.low=0.1) %>%
14+
error_bar(x=1:3, y=c(3,1,2), x.low=.2, x.high=.2, col="red",lwd=3) %>%
15+
callouts(x=1, y=2.8, lwd=2, angle=250, labels="Weird data") %>%
16+
title("Graphing Fun")
17+
demoPlot
18+
19+
20+
## ----echo=TRUE, message=FALSE--------------------------------------------
21+
22+
sites <- unique(MaumeeDV$site_no)
23+
dates <- sapply(sites, function(x) MaumeeDV$Date[which(MaumeeDV$site_no==x)], USE.NAMES=TRUE)
24+
flow <- sapply(sites, function(x) MaumeeDV$Flow[which(MaumeeDV$site_no==x)], USE.NAMES=TRUE)
25+
pH <- sapply(sites, function(x) MaumeeDV$pH_Median[which(MaumeeDV$site_no==x)], USE.NAMES=TRUE)
26+
Wtemp <- sapply(sites, function(x) MaumeeDV$Wtemp[which(MaumeeDV$site_no==x)], USE.NAMES=TRUE)
27+
28+
29+
## ----echo=TRUE, fig.cap="Fig. 1 Simple flow timeseries using `gsplot`.", fig.width=6, fig.height=6----
30+
site <- '04193500'
31+
demoPlot <- gsplot(mgp=c(2.75, 0.3, 0.0)) %>%
32+
lines(dates[[site]], flow[[site]], col="royalblue") %>%
33+
title(main=paste("Site", site), ylab="Flow, ft3/s") %>%
34+
grid()
35+
demoPlot
36+
37+
38+
## ----echo=TRUE, fig.cap="Fig. 2 Simple flow timeseries with a logged y-axis using `gsplot`.", fig.width=6, fig.height=6----
39+
site <- '04193500'
40+
options(scipen=5)
41+
demoPlot <- gsplot(mgp=c(3, 0.3, 0.0)) %>%
42+
lines(dates[[site]], flow[[site]],
43+
col="royalblue", log='y',
44+
ylab= expression(paste("Discharge in ",ft^3/s))) %>%
45+
title(main=paste("Site", site)) %>%
46+
grid(equilogs=FALSE)
47+
demoPlot
48+
49+
50+
## ----echo=TRUE, fig.cap="Fig. 3 (a) pH vs water temperature, (b) pH timeseries, (c) water temperature timeseries.", fig.width=6, fig.height=6----
51+
site <- '04193490'
52+
plot1 <- gsplot() %>%
53+
points(Wtemp[[site]], pH[[site]], col="black")%>%
54+
title(main=paste("Site", site), xlab="Water Temperature (deg C)", ylab="pH")
55+
plot2 <- gsplot() %>%
56+
lines(dates[[site]], pH[[site]], col="seagreen")%>%
57+
title(main="", xlab="time", ylab="pH")
58+
plot3 <- gsplot() %>%
59+
lines(dates[[site]], Wtemp[[site]], col="orangered")%>%
60+
title(main="", xlab="time", ylab="Water Temperature (deg C)")
61+
62+
layout(matrix(c(1,2,3), byrow=TRUE, nrow=3))
63+
plot1
64+
plot2
65+
plot3
66+
67+
68+
## ----echo=TRUE, fig.cap="Fig. 4 Water temperature timeseries on primary y-axis with pH timeseries on secondary y-axis.", fig.width=6, fig.height=6----
69+
site <- '04193490'
70+
demoPlot <- gsplot(mar=c(7.1, 4.1, 4.1, 4.1)) %>%
71+
lines(dates[[site]], Wtemp[[site]], col="orangered",
72+
legend.name="Water Temperature", ylab='Water Temperature (deg C)') %>%
73+
lines(dates[[site]], pH[[site]], col="seagreen", side=4,
74+
legend.name="pH", ylab='pH (pH Units)') %>%
75+
title(main=paste("Site", site), xlab='time') %>%
76+
legend(location="below")
77+
demoPlot
78+
79+
80+
## ----echo=TRUE, fig.cap="Fig. 5 Initial plot of water temperature timeseries.", fig.width=6, fig.height=6----
81+
# initially plot the data
82+
site <- '04193490'
83+
demoPlot <- gsplot() %>%
84+
lines(dates[[site]], Wtemp[[site]], col="orangered") %>%
85+
title(main=paste("Site", site), xlab='time', ylab='Water Temperature (deg C)')
86+
demoPlot
87+
88+
## ----echo=TRUE, fig.cap="Fig. 6 Plot of water temperature timeseries with 'Missing Data' callout retroactively added.", fig.width=6, fig.height=6----
89+
# notice the missing data from ~ 1991 through ~2011 and add a callout
90+
demoPlot <- callouts(demoPlot, x=as.Date("2000-01-01"), y=10,labels="Missing Data")
91+
demoPlot
92+
93+

0 commit comments

Comments
 (0)