我见过this post,但是我正在努力将其转换为对数栅格。
例如:
library(tidyverse)
a <- tibble(x = rep(10^seq(-2, 2), 5),
y = rep(10^seq(-2, 2), each = 5),
z = runif(25))
a %>%
ggplot(aes(x, y)) +
geom_raster(aes(fill = z)) +
scale_x_log10() +
scale_y_log10()
例如,现在如何获得带有y = 0.1
的行周围的框?还是只有一个瓷砖的盒子?
我知道对数刻度上两点之间的半点是通过几何平均值计算的。
更新:
对于上面的示例,该解决方案似乎有效,但是如果x
和y
看起来有些不同,例如:
n_x <- 10^seq(log10(6), log10(24*365), by = 0.1)/365
n_y <- 10^seq(-1, 3, by = 0.1)
a <- tibble(x = rep(n_x, length(n_y)),
y = rep(n_y, each = length(n_x)),
z = runif(length(n_x)*length(n_y)))
h <- a$y[which.min(abs(a$y - 1.14*24))]
h.tb <- a %>%
dplyr::filter(y == h)
a %>%
ggplot(aes(x = x, y = y)) +
geom_raster(aes(fill = z)) +
scale_x_log10() +
scale_y_log10(breaks = c(1, h, 100), labels = c('1', 'h', '100')) +
geom_tile(data = h.tb, fill = NA, colour = "black", size = 2)
有趣的是,h.tb
包含正确的数据。
答案 0 :(得分:1)
例如,假设您要在y = 0.1行中标记图块,对于x <10,则可以像下面这样添加geom_tile()
:
p1 <- a %>%
ggplot(aes(x = x, y = y)) +
geom_raster(aes(fill = z)) +
scale_x_log10() +
scale_y_log10() +
geom_tile(data = . %>% filter(y == 0.1 & x < 10), # filter dataset for desired tiles
fill = NA, # make tiles transparent
colour = "black", size = 2) # aesthetic choices
p1
如果您希望这些图块形成一个矩形,我想不出一个同样简单的方法,但是可以做到。
# continuing from above, using the geom_tile layer from p1 to
# obtain the correct tile dimensions, then transform all measures
# back to the non-log form
p1.data <- layer_data(p1, 2) %>%
summarise(xmin = min(xmin), xmax = max(xmax),
ymin = min(ymin), ymax = max(ymax)) %>%
mutate_all(function(x) 10^x)
> p1.data
xmin xmax ymin ymax
1 0.003162278 3.162278 0.03162278 0.3162278
# replace the geom_tile() layer earlier with geom_rect() & the new data
a %>%
ggplot(aes(x = x, y = y)) +
geom_raster(aes(fill = z)) +
scale_x_log10() +
scale_y_log10() +
geom_rect(data = p1.data,
aes(xmin = xmin, xmax = xmax,
ymin = ymin, ymax = ymax),
inherit.aes = FALSE, fill = NA,
colour = "black", size = 2)
答案 1 :(得分:0)
对于更新后的示例,只需将height = 0.1
添加到geom_tile
,其中height
的值需要适合by
中的n_y
的值。
n_x <- 10^seq(log10(6), log10(24*365), by = 0.1)/365
step_y <- 0.1
n_y <- 10^seq(-1, 3, by = step_y)
a <- tibble(x = rep(n_x, length(n_y)),
y = rep(n_y, each = length(n_x)),
z = runif(length(n_x)*length(n_y)))
h <- a$y[which.min(abs(a$y - 1.14*24))]
h.tb <- a %>%
dplyr::filter(y == h)
a %>%
ggplot(aes(x = x, y = y)) +
geom_raster(aes(fill = z)) +
scale_x_log10() +
scale_y_log10(breaks = c(1, h, 100), labels = c('1', 'h', '100')) +
geom_tile(data = h.tb, fill = NA, colour = "black", size = 2, height = step_y)