Skip to content

Commit bd6f6ab

Browse files
committed
fix bug reporting multiple obs for 2 panel factors
1 parent 630e0e5 commit bd6f6ab

5 files changed

Lines changed: 64 additions & 7 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: desplot
33
Title: Plotting Field Plans for Agricultural Experiments
4-
Version: 1.10
4+
Version: 1.11
55
Authors@R:
66
person("Kevin", "Wright", , "kw.stat@gmail.com", role = c("aut", "cre", "cph"),
77
comment = c(ORCID = "0000-0002-0617-8673"))

NEWS.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
# desplot 1.11 - unpublished
1+
# desplot 1.11 ()
22

33
* Switch to MIT license.
44

5+
* Documentation pages now created via Github Actions.
6+
7+
* Fix the checking for multiple cell values when there are 2 panel factors.
8+
9+
510
# desplot 1.10 (2023-03-01)
611

712
* One-row panels no longer have whitespace. Issue #9.
13+
814
* Replace `aes_string(x=x.string)` with `aes(x=.data[[x.string]])` etc.
915

1016

1117
# desplot 1.9 (2021-10-30)
1218

1319
* Tweaks to `ggdesplot` output.
20+
1421
* Remove LazyData from DESCRIPTION.
1522

1623

1724
# desplot 1.8 (2020-10-21)
1825

1926
* Bug fix for `dq` with multiple panels.
27+
2028
* Use `inherits` to check class #4.
2129

2230

R/desplot.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,21 @@ desplot <- function(data,
490490
fac2num <- function(x) as.numeric(levels(x))[x]
491491
if(is.factor(data[[x.string]])) data[[x.string]] <- fac2num(data[[x.string]])
492492
if(is.factor(data[[y.string]])) data[[y.string]] <- fac2num(data[[y.string]])
493-
data <- .addLevels(data, x.string, y.string, panel.string)
493+
#data <- .addLevels(data, x.string, y.string, panel.string)
494494

495-
# Check for multiple values
496-
if(is.null(panel.string)){
495+
# Check for multiple values for each cell.
496+
if(is.null(ff$cond)) {
497+
# no factor for panels
497498
tt <- table(data[[x.string]], data[[y.string]])
499+
} else if(length(ff$cond) == 1L) {
500+
# one conditioning factor
501+
tt <- table(data[[x.string]], data[[y.string]], data[[ff$cond[1]]])
502+
} else if(length(ff$cond)==2L) {
503+
# two conditioning factors
504+
tt <- table(data[[x.string]], data[[y.string]], data[[ff$cond[1]]], data[[ff$cond[2]]])
498505
} else {
499-
tt <- table(data[[x.string]], data[[y.string]], data[[panel.string]])
506+
message("Not checking for multiple data for each x/y/panel combination")
507+
tt <- NULL
500508
}
501509
if(any(tt>1))
502510
warning("There are multiple data for each x/y/panel combination")

tests/testthat/test_desplot.R

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test_that("num,col,text", {
4141
})
4242
})
4343

44-
test_that("function `cleanup` checks variables", {
44+
test_that("function cleanup checks variables", {
4545
expect_error( desplot(oats35, yield~col+row, num=junk) )
4646
expect_error( desplot(oats35, yield~col+row, col=junk) )
4747
expect_error( desplot(oats35, yield~col+row, text=junk) )
@@ -134,6 +134,30 @@ test_that("subset", {
134134
})
135135
})
136136

137+
## ---------------------------------------------------------------------------
138+
139+
test_that("If a cell has multiple observations, issue a warning.",{
140+
# No panel factor
141+
dat0 <- expand.grid(col=1:2, row=1:2)
142+
dat0$y <- rnorm(nrow(dat0))
143+
dat0 <- rbind(dat0, dat0[1,]) # Create one cell that has 2 observations
144+
145+
# One panel factor
146+
dat1 <- expand.grid(state=c("S1","S2"), col=1:2, row=1:2)
147+
dat1$y <- rnorm(nrow(dat1))
148+
dat1 <- rbind(dat1, dat1[1,]) # Create one cell that has 2 observations
149+
150+
# Two panel factors
151+
dat2 <- expand.grid(state=c("S1","S2"), loc=c("L1","L2","L3"), col=1:2, row=1:2)
152+
dat2$y <- rnorm(nrow(dat2))
153+
dat2 <- rbind(dat2, dat2[1,]) # Create one cell that has 2 observations
154+
155+
expect_warning( desplot(dat0, y~col*row) )
156+
expect_warning( desplot(dat1, y~col*row|state) )
157+
expect_warning( desplot(dat2, y~col*row|state*loc) )
158+
})
159+
160+
137161
# ----------------------------------------------------------------------------
138162

139163

vignettes/desplot_examples.Rmd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ Note the largest positive/negative residuals are adjacent to each other,
5353
perhaps caused by the original data values being swapped. Checking with
5454
experiment investigators (managers, data collectors, etc.) is recommended.
5555

56+
# Infrequently asked questions
5657

58+
## How do I change the ordering of panels?
59+
60+
Make sure that the panel variable is a factor and then change the levels of the factor.
61+
62+
In the example below, the first three panels are set to the levels C1, C3, C5. The other levels remain in the same (relative) order.
63+
64+
```{r panelorder, eval=FALSE}
65+
library(agridat)
66+
library(desplot)
67+
data(besag.met)
68+
desplot(besag.met, yield~col*row|county, main="default county ordering")
69+
library(forcats)
70+
besag.met <- transform(besag.met,
71+
county=fct_relevel(county, c("C1","C3","C5")))
72+
desplot(besag.met, yield~col*row|county, main="custom county ordering")
73+
```
5774
## References
5875

0 commit comments

Comments
 (0)