rm(list = ls())
library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------- tidyverse 1.3.0 --
## √ ggplot2 3.3.2 √ purrr 0.3.4
## √ tibble 3.0.3 √ dplyr 1.0.1
## √ tidyr 1.1.2 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.5.0
## -- Conflicts ---------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
set.seed(12345)
dat <-
data.frame(
Row = rep(x = LETTERS[1:5], times = 10)
, Col = rep(x = LETTERS[1:10], each = 5)
, Y = rnorm(n = 50, mean = 0, sd = 1)
)
head(dat)
## Row Col Y
## 1 A A 0.5855288
## 2 B A 0.7094660
## 3 C A -0.1093033
## 4 D A -0.4534972
## 5 E A 0.6058875
## 6 A B -1.8179560
library(ggplot2)
######################plot_continuous
p <- ggplot(data = dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y), colour = "white") +
scale_fill_gradient(low = "white", high = "steelblue")
p

dat$Y1 <- cut(dat$Y,breaks = c(-Inf,-3:3,Inf),right = FALSE)
head(dat)
## Row Col Y Y1
## 1 A A 0.5855288 [0,1)
## 2 B A 0.7094660 [0,1)
## 3 C A -0.1093033 [-1,0)
## 4 D A -0.4534972 [-1,0)
## 5 E A 0.6058875 [0,1)
## 6 A B -1.8179560 [-2,-1)
######################plot_1
ggplot(data = dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y1, height = 1), colour = "red",size =1) +
scale_fill_manual(values = c("red","green","black","darkred","darkgreen","white"))+
scale_x_discrete(expand = c(0,0))+
scale_y_discrete(expand = c(0,0)) +
#coord_fixed(ratio=1) +
theme(panel.background = element_blank(),
axis.ticks = element_line(size= 0.5),
axis.ticks.length = unit(10, "pt"),
axis.text = element_text(colour="black",size=16,face="plain",family = "sans"),
axis.title = element_text(colour="black",size=16,face="plain",family = "sans"),
legend.background = element_blank(),
legend.key = element_rect(colour = NA, fill = NA))

######################plot_2
ggplot(data = dat, aes(x = Row, y = Col)) +
geom_tile(aes(fill = Y1, height = 1), colour = "red",size =1) +
scale_fill_manual(values = c("red","green","black","darkred","darkgreen","white"))+
scale_x_discrete(expand = c(0,0))+
scale_y_discrete(expand = c(0,0),breaks = c("A", "C", "D","F","I")) +
#coord_fixed(ratio=1) +
theme(panel.background = element_blank(),
axis.ticks = element_line(size= 0.5),
axis.ticks.length = unit(10, "pt"),
axis.text = element_text(colour="black",size=16,face="plain",family = "sans"),
axis.title = element_text(colour="black",size=16,face="plain",family = "sans"),
legend.background = element_blank(),
legend.key = element_rect(colour = NA, fill = NA))
