文章目录
前言
不想干正事儿,搞搞 “ 颜色 ”😑
创建两个颜色之间的 Color Range
例子:创建蓝色->红色之间的 Color Range
首先,使用 colorRampPalette
定义一个函数,包括起始和终止的颜色:
fun_color_range <- colorRampPalette(c("#1b98e0", "red"))
PS: Color code
然后,我们可以使用这个函数,创建从 #1b98e0 色到红色的Corlor Range,先来100个吧:
my_colors <- fun_color_range(100)
所有的100个颜色都会存储在my_colors这个对象中,感兴趣的自己看里面的Color Code是啥样……这里直接用图展示():
plot(1:100, # Example plot
pch = 16,
col = my_colors)
创建一组渐变的调色板
首先,定义变量my_colours
:
my_colours <- c("#f2edee","#8a79f4","#f0d359","#ff70c3","#53ecc0",
"#af0043","#bae179","#ec5646","#4f9059","#af3c00")
颜色取自一个网站:I want hue,大概这样:
然后,利用上边的my_colours创建渐变色:
library(tidyverse)
# 定义最低值代表的颜色
gradient_base <- my_colours[1]
# 创建渐变色List
my_gradients <- map(my_colours[2:10],
function(x) colorRampPalette(c(gradient_base,x))(5))
其中一个大概的样子:
使用
数据:worm-sample-data.csv,可以先看一眼数据啥样
glimpse(dat)
# Observations: 529
# Variables: 4
# $ concentration <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
# $ drug <fct> Control, Control, Control, Control, Control, Con...
# $ experiment <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
# $ movements <dbl> 281, 275, 244, 276, 255, 250, 265, 259, 279, 282...
画一下 “1 uM treatments”
颜色就用前面定义的my_colours:
dat %>% filter(concentration == 0 | concentration == 1) %>%
group_by(drug,concentration) %>%
ggplot(aes(drug,movements, fill = drug)) +
stat_summary(geom = "bar", fun.y = "mean") +
stat_summary(geom = "errorbar", fun.data = "mean_se", width = .2) +
scale_fill_manual(values = my_colours) +
ggtitle("1 uM treatment comparison") +
xlab("") +
theme_minimal()
用自定义的渐变色
dat %>% filter(drug == "Control" | drug == "Drug 1") %>%
ggplot(aes(concentration,movements, fill = concentration)) +
stat_summary(geom = "bar", fun.y = "mean") +
stat_summary(geom = "errorbar", fun.data = "mean_se", width = .2) +
scale_fill_manual(values = my_gradients[[1]]) + # Drug 1 is gradient 1
ggtitle("Drug 1 treatment comparison") +
theme_minimal()
再来一个拼图的
# Load cowplot for mutli-panel figures
library(cowplot)
p1 <- dat %>% filter(drug == "Control" | drug == "Drug 1") %>%
ggplot(aes(concentration,movements, fill = concentration)) +
stat_summary(geom = "bar", fun.y = "mean") +
stat_summary(geom = "errorbar", fun.data = "mean_se", width = .2) +
scale_fill_manual(values = my_gradients[[1]]) +
ggtitle("Drug 1 treatment comparison") +
theme_minimal()
# Assign Drug 3 plot to p3
p3 <- dat %>% filter(drug == "Control" | drug == "Drug 3") %>%
group_by(experiment) %>%
ggplot(aes(concentration,movements, fill = concentration)) +
stat_summary(geom = "bar", fun.y = "mean", position = "dodge") +
stat_summary(geom = "errorbar", fun.data = "mean_se", width = .2) +
scale_fill_manual(values = my_gradients[[3]])
ggtitle("Drug 3 treatment comparison") +
theme_minimal()
# 拼图
plot_grid(p1,p3)