渐变色【R Color】

前言

不想干正事儿,搞搞 “ 颜色 ”😑

创建两个颜色之间的 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)

在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值