我使用ggplot的geom_tile函数来可视化一些空间数据。我有一个连续变量
region_relative_rainfall
离散变量
region
. 我想创建一个清晰的绘图,有对比色为每个层次的离散变量。在每一级的离散变量中,连续变量的顺序颜色相同。我只知道如何用下面的代码更改填充和颜色,但没有我想的那么清楚。任何小费都会很感激的。
# geom_tile question
library(ggplot2)
library(dplyr)
set.seed(123)
n_row = 10
n_col = 20
df = expand.grid(1:n_row, 1:n_col)
colnames(df) = c("y","x")
n = n_row * n_col
k = 5
df$region = sample(x = letters[1:k], size = n, replace = T)
df$rainfall = rlnorm(n = n, log(13), 0.4)
## normalise rainfall by region, to sum = 1 for each region
df <- df %>%
group_by(region) %>%
mutate("region_relative_rainfall" =rainfall / sum(rainfall))
## Current plot, not quite what I want
ggplot(df, aes(x = x, y = y, fill = region_relative_rainfall, color = region)) +
geom_tile() +
theme(panel.grid = element_blank(),
axis.text = element_blank()) +
scale_y_reverse( lim=c(n_row,1))