0

I am working to plot a dataset of snow depth in a grid, with depth as a continuous variable. But, some plots in the grid were partially covered. These are divided into plots >50% and <50% covered in the same variable as the depth measurements, marked as 'over' and 'under'.

When I try to plot the data with ggplot 2 using:

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

I of course get the error:

Error: Discrete value supplied to continuous scale

How can I include this discrete data and give them a clear colour label, so all the information is shown in the same image?

Sil
  • 11
  • 1

1 Answers1

2

Edit:

After thinking about it some more, one approach could be to use a pattern from ggpattern on the covered plots:

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

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
  • That comes close but is not exactly what I need here (although it comes in handy for something else) My problem here is that it's not both continuous and discrete data in separate fields, but EITHER continuous (snow depth) OR discrete (partially covered, <50% or >50%). This data is recorded under the same variable. I could split them up in two, but then I would have an NA in either field. And unfortunately it seems like I can't use ggpattern (not available for version 3.6.2). – Sil May 20 '20 at 11:52