R/plot_structure.R
changed.
Other files ignored by Codecov
man/plot_structure.Rd
has changed.
15 | 15 | #' @rdname plot_structure |
|
16 | 16 | #' @importFrom graphics Axis barplot layout |
|
17 | 17 | #' @importFrom utils modifyList |
|
18 | + | #' @importFrom sp coordinates<- CRS proj4string<- SpatialPolygonsDataFrame |
|
19 | + | #' @importFrom sp coordinates<- over CRS proj4string merge |
|
18 | 20 | #' @return Returns no value, just visualize structure plot! |
|
19 | 21 | #' @export |
|
20 | - | plot_structure <- function(omega, shp, by = NULL, col=hcl.colors(12), ...) { |
|
22 | + | plot_structure <- function(omega, shp, by = NULL, col=hcl.colors(15), ...) { |
|
21 | 23 | ||
22 | 24 | #shp <- obj$shp |
|
23 | 25 | #omega <- obj$omega |
26 | 28 | s1 <- subset(shp, shp$grids %in% index) |
|
27 | 29 | ||
28 | 30 | if (!is.null(by)) { |
|
29 | - | s1 <- s1[, c("grids", by)] |
|
30 | - | names(s1) <- c("grids", "by") |
|
31 | - | sx <- split(s1, f=s1$by) |
|
32 | - | vv <- as.numeric(matrix(lapply(sx, function(x) nrow(x)/nrow(s1)))) |
|
31 | + | if (inherits(by, "SpatialPolygonsDataFrame")) { |
|
32 | + | w <- s1[, "grids"] |
|
33 | + | suppressWarnings(proj4string(by) <- proj4string(w)) |
|
34 | + | r <- suppressWarnings(cbind(as.data.frame(w), sp::over(w, by))) |
|
35 | + | d <- merge(w, r, by="grids") |
|
36 | + | d <- d[, c(1, 2)] |
|
37 | + | names(d) <- c("grids", "by") |
|
38 | + | d <- d[!is.na(d@data$by),] |
|
39 | + | } else { |
|
40 | + | d <- s1[, c("grids", by)] |
|
41 | + | names(d) <- c("grids", "by") |
|
42 | + | } |
|
43 | + | ||
44 | + | tmp <- names(which(table(d$by)<2)) |
|
45 | + | if(length(tmp)>0) { |
|
46 | + | print("Dropping sites because they have only one grid cell:") |
|
47 | + | print(tmp) |
|
48 | + | } |
|
49 | + | ||
50 | + | d <- subset(d, !(d$by %in% tmp)) |
|
51 | + | ||
52 | + | sx <- split(d, f=d$by) |
|
53 | + | vv <- as.numeric(matrix(lapply(sx, function(x) nrow(x)/nrow(d)))) |
|
33 | 54 | ||
34 | 55 | nf <- layout(matrix(1:length(sx), ncol=1), heights=vv) |
|
35 | 56 |
40 | 61 | par(mgp=c(3,0,0)) |
|
41 | 62 | par(mar=c(0, 5, 0, 1)) |
|
42 | 63 | barplot(z1, horiz=TRUE, col=col, |
|
43 | - | border=NA, space=0, las=1, |
|
44 | - | axes=FALSE, ylab=x$by[1], cex.lab=0.75) |
|
45 | - | ||
46 | - | }))) |
|
64 | + | border = if (nrow(d) < 200) "black" else NA, |
|
65 | + | space=0, las=1, |
|
66 | + | axes=FALSE, ylab=x$by[1], cex.lab=0.75)}))) |
|
47 | 67 | } else { |
|
48 | 68 | ind1 <- intersect(s1$grids, row.names(omega)) |
|
49 | 69 | y1 <- omega[ind1,] |
Files | Coverage |
---|---|
R | 7.80% |
Project Totals (37 files) | 7.80% |