本文内容:
pkgs = c('tidyverse', 'forcats', 'gtools', 'ggplot2', 'ggpubr', 'cowplot',
'scales', 'ggsci', 'viridis', 'hrbrthemes', 'Cairo', 'common')
# install.packages('pkgs')
inst = lapply(pkgs, library, character.only = TRUE)
strings <- paste0('C', 1:5)
data <- data.frame(
day = 1:12,
type.name = c(rep(c('A', 'B'), each = 6)),
condition = c('Control Group', 'Experimental Group1', 'Experimental Group2'),
Dilution = rep(c(1:2), each = 3),
Dilution1 = sample(strings, 12, replace = T),
Dilution2 = sample(strings, 12, replace = T),
Dilution3 = sample(strings, 12, replace = T)
)
data %>%
slice(1:3)
p <- data %>%
gather(key, value, paste0('Dilution', 1:3)) %>%
unite(condition, condition, Dilution, sep = ' ') %>%
ggplot(aes(x = key, y = condition)) +
geom_tile(aes(fill = value), color = 'white', size = 1)
p
p1 <- p + coord_fixed()
p1
p2 <- p1 + facet_wrap(~type.name)
p2
p3 <- p2 + geom_text(aes(label = value), color = 'black', size = 4)
p3
p4 <- p3 + scale_y_discrete(labels = label_wrap(10))
p4
p4 + theme_classic2(base_size = 16) + scale_fill_simpsons() +
theme(axis.text.x = element_text (angle = 45, vjust = 1, hjust=1))
# 用点绘制热图,颜色为分类变量
data %>%
gather(key, value, paste0('Dilution', 1:3)) %>%
unite(condition, condition, Dilution, sep = ' ') %>%
ggplot(aes(x = key, y = condition)) +
coord_fixed() +
facet_wrap(~ type.name) +
geom_point(aes(size = value, color = value)) +
theme_bw() +
theme(panel.border = element_rect(fill=NA, color="black", size=1, linetype="solid")) +
theme(panel.grid = element_blank(),
axis.text.x =element_text(angle =90, hjust =0.5, vjust = 0.5)) +
theme_classic2(base_size = 16) +
scale_color_simpsons() +
theme(axis.text.x = element_text (angle = 45, vjust = 1, hjust=1))
# 用点绘制热图,颜色为连续变量
data.d <- data %>%
gather(key, value, paste0('Dilution', 1:3)) %>%
unite(condition, condition, Dilution, sep = ' ') %>%
# charactor to factor, factor to numeric
mutate(value = as.numeric(factor(value)))
data.d %>%
ggplot(aes(x = key, y = condition)) +
coord_fixed() +
facet_wrap(~ type.name) +
geom_point(aes(size = value, color = value)) +
theme_bw() +
theme(panel.border = element_rect(fill=NA, color="black", size=1, linetype="solid")) +
theme(panel.grid = element_blank(),
axis.text.x =element_text(angle =90, hjust =0.5, vjust = 0.5)) +
theme_classic2(base_size = 16) +
scale_color_gradientn(colours = c('#6699CC','#FFFF99','#CC3333')) +
theme(axis.text.x = element_text (angle = 45, vjust = 1, hjust=1))
参考