在变量中绘制连续数据和离散数据的图表

问题描述 投票:0回答:1

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

当我尝试用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

我怎样才能把这些离散的数据包括进去,并给它们一个清晰的颜色标签,这样所有的信息就会显示在同一个图像中?

r ggplot2 discrete-mathematics continuous
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")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.