我已经为此解决了一个问题,但是它是丑陋的,临时的并且无法概括。我认为必须有更好的方法。我们的研究区域分为100mx100m的网格,名为3-8的列和名为C-J的行。每个块分为四个四边形,每个四边形分为四个子四角形,因此子四角形的名称类似于“ 4F23”。划分模式如下:
11 12 21 22
13 14 23 24
31 32 41 42
33 34 43 44
不同的数据集使用三种分辨率中的任意一种都是合适的,所以我想要可以处理“ 4F”,“ 4F1”和“ 4F34”的东西。为了在该区域的热图/叶绿素中显示值,我需要一种方法来表示与ggplot更正交的嵌套方案,或者更好地告诉ggplot以便它知道如何解释部分标签本身。我想要的是一种简单的方法来绘制我们所有研究的地图。我能想到的最好的方法是使用转换函数生成XY,并将其绑定到数据框。
toyDF <- tibble(SECT = c('3E1', '5G3', '8H4'), HT = c(22,6,15))
# Translator function
SACoords <- function(sqr) {
sqVec <- substring(sqr, seq(1, nchar(sqr), 1), seq(1, nchar(sqr), 1))
rws <- 'JIHGFEDC'
cl <- (as.integer(sqVec[1]) - 3) * 100
rw <- (as.integer(gregexpr(sqVec[2], rws)) - 1) * 100
qd <- ifelse(!is.na(sqVec[3]),
list(c(0,50), c(50,50), c(0,0), c(50,0))[as.integer(sqVec[3])],
c(0,0))
sq <- ifelse(!is.na(sqVec[4]),
list(c(0,25), c(25,25), c(0,0), c(25,0))[as.integer(sqVec[4])],
c(0,0))
coords <- data.frame(c(cl, rw), qd, sq)
rowSums(coords, na.rm=TRUE)
}
#> SACoords('8C24') # Test
#[1] 575 750 # Yes
# Mash in the coordinates (as lists)
toyXY <- toyDF %>%
mutate(coords = sapply(SECT, SACoords, simplify=FALSE))
# Got the coords in, but as lists -- difficult to work with;
# but can't mutate() into two columns with one operation, so
# redo it this ungainly way:
toyXY[,4:5] <- matrix(unlist(sapply(toyDF$SECT, SACoords)),
ncol=2, byrow=TRUE)
names(toyXY)[4:5] <- c('Xcoor', 'Ycoor')
# And finally to plot (in reality many observations per SECT)
toyXY %>% group_by(SECT) %>%
mutate(MHT = mean(HT)) %>%
ggplot(aes(xmin=Xcoor, xmax=Xcoor + 50, ymin=Ycoor, ymax=Ycoor+50)) +
geom_rect(aes(fill = MHT))
有了完整的数据集,它可以产生我想要的东西,但是很糟糕。我最想要的是(我认为)将我的SACoords()
转换为可以插入ggplot(aes())
调用中的转换,以便它可以有效地理解SECT
标签,并希望允许我使用geom_raster
代替geom_rect
,并避免必须根据给定研究的分辨率调整xmin / xmax及其尴尬的常数。次佳的可能是研究图的模板表示-2d矩阵,24x32?列表列表的6x8矩阵? -但我不知道如何告诉ggplot读取它。还是我真的应该将所有这些包装在一个可以处理所有事情的更大的功能中?
答案 0 :(得分:0)
如果包含学习区域的插图,将会更加清楚,但这是我对所寻找内容的最佳猜测。如果我的理解是正确的,则可以在dplyr
包的管道操作中执行所有翻译,这将使对代码中每个步骤所发生的事情的解释更加容易。
请注意,出于说明目的,我使用了不同的SECT
值。注释中的解释:
library(dplyr)
library(ggplot2)
# modify toyDF to include sections of different sizes
toyDF <- tibble::tibble(SECT = c("3E", "5G3", "8C24"),
HT = c(22, 6, 15))
toyDF %>%
mutate(sqr = stringr::str_pad(SECT, 4, side = "right", pad = " ")) %>%
tidyr::separate(sqr, into = c("x", "y", "quadrat", "subquadrat"), sep = 1:3) %>%
# convert the first two letters of SECT into x/y coordinates for the centre of the area
mutate(x = factor(x, levels = as.character(3:8)),
y = factor(y, levels = LETTERS[10:3])) %>%
mutate_at(vars(x, y),
function(i) as.integer(i) * 100 - 50) %>%
# adjust coordinates for quadrat, if applicable
mutate(x = case_when(quadrat %in% c("1", "3") ~ x - 25,
quadrat %in% c("2", "4") ~ x + 25,
TRUE ~ x),
y = case_when(quadrat %in% c("1", "2") ~ y + 25,
quadrat %in% c("3", "4") ~ y - 25,
TRUE ~ y)) %>%
# further adjust coordinates for subquadrat, if applicable
mutate(x = case_when(subquadrat %in% c("1", "3") ~ x - 12.5,
subquadrat %in% c("2", "4") ~ x + 12.5,
TRUE ~ x),
y = case_when(subquadrat %in% c("1", "2") ~ y + 12.5,
subquadrat %in% c("3", "4") ~ y - 12.5,
TRUE ~ y)) %>%
# specify appropriate width for each cell, depending on whether
# subquadrat / quadrat has been defined
mutate(width = case_when(subquadrat != " " ~ 25,
quadrat != " " ~ 50,
TRUE ~ 100)) %>%
ggplot(aes(x = x, y = y, fill = HT)) +
geom_tile(aes(height = width, width = width)) +
scale_x_continuous(breaks = seq(50, 550, by = 100),
labels = as.character(3:8),
expand = c(0, 0)) +
scale_y_continuous(breaks = seq(50, 750, by = 100),
labels = LETTERS[10:3],
expand = c(0, 0)) +
coord_equal(xlim = c(0, 600), ylim = c(0, 800)) +
theme_bw() +
theme(panel.grid.major = element_blank(),
axis.ticks = element_blank())