0

我正在努力在网格中绘制雪深数据集,深度作为连续变量。但是,网格中的一些地块被部分覆盖。这些分为 >50% 和 <50% 的图,覆盖在与深度测量相同的变量中,标记为“上”和“下”。

当我尝试使用 ggplot 2 绘制数据时:

myplot <- ggplot()+
geom_tile(data=table, aes(x=factor(Row), y=Colum, fill= Snow_depth))+
scale_fill_gradient(low="#0066CC", high="#FF3333")

我当然得到错误:

Error: Discrete value supplied to continuous scale

如何包含这些离散数据并给它们一个清晰的颜色标签,以便所有信息都显示在同一张图像中?

4

1 回答 1

2

编辑:

在考虑了更多之后,一种方法可能是使用ggpattern覆盖图上的模式:

set.seed(3)
table <- data.frame(Row = rep(1:9,times=9), Colum = rep(1:9,each=9),
                   Snow_depth = runif(81,10,100),
                   Cover = as.logical(round(runif(81,0,1),0)))

#remotes::install_github("coolbutuseless/ggpattern")
library(ggpattern)
library(ggplot2)
ggplot(data=table, aes(x=as.factor(Row), y=as.factor(Colum))) +
  geom_tile(aes(fill= Snow_depth)) +
  scale_fill_gradient(low="#0066CC", high="#FF3333") +
  geom_tile_pattern(aes(pattern_alpha = Cover),
                    fill = NA, pattern = 'crosshatch',
                    pattern_fill = "black",
                    pattern_angle = 45,
                    pattern_density = 0.1,
                    pattern_spacing = 0.025,
                    pattern_key_scale_factor = 0.5) +
  scale_pattern_alpha_discrete(range = c(0,0.5), labels = c("No","Yes")) +
  labs(x = "Row",y = "Column")

在此处输入图像描述

于 2020-05-18T14:11:40.657 回答