理解ggplot2颜色的标尺函数(scale_color_*,scale_color_*)

关于颜色的标尺函数在ggplot2中竟然多达22种,如此多的标尺函数未免使人感到疑惑

library(ggplot2)
grep('scale_color_*', x = ls('package:ggplot2'), value = TRUE)#在这里fill同理

[1] “scale_color_binned” “scale_color_brewer”
[3] “scale_color_continuous” “scale_color_date”
[5] “scale_color_datetime” “scale_color_discrete”
[7] “scale_color_distiller” “scale_color_fermenter”
[9] “scale_color_gradient” “scale_color_gradient2”
[11] “scale_color_gradientn” “scale_color_grey”
[13] “scale_color_hue” “scale_color_identity”
[15] “scale_color_manual” “scale_color_ordinal”
[17] “scale_color_steps” “scale_color_steps2”
[19] “scale_color_stepsn” “scale_color_viridis_b”
[21] “scale_color_viridis_c” “scale_color_viridis_d”

对颜色标尺函数的理解

让我们直入正题,虽然ggplot2拥有多达22种的标尺函数,但我们可以根据关键性质(一共分为三种),这样我们可以大体知道在不同的情形下具体使用哪个标尺函数。

  1. 数据映射的自然属性:
    aes()图形映射里描述的数据是连续的还是离散的?连续的典型如时间,温度,长度,离散的典型如分类结果ABCD。
  2. 颜色空间:
    简单来说就是你在画图内使用的调色板,在默认情况下ggplot2已经给你设置了调色板,此外ggplot2还内置了RcolorBrewer调色板以及Viridis调色板
  3. 层级的控制:
    一旦指定映射数据是否离散,规定颜色空间之后,用户就需要在颜色的层级上描述差异,例如*_continuous()*_gradient*_gradient2()*_gradientn()

连续的标尺

我们以连续的标尺开始示例,在映射数据是连续的时候,我们选择continous函数族,这些函数可以进一步指定它们是否是分箱的,(分箱是指将连续的变量分组为数个分组,每个组都被指定到特定的颜色中),在后续示例你会在图例中注意到分箱和连续颜色的区别。
默认情况下:

library(ggplot2)
cont <- ggplot(mtcars, aes(mpg, disp, color=cyl)) + geom_point(size=4)

cont + scale_color_continuous()# scale_color_continuous可以不用添加

在这里插入图片描述
需要指出的是,scale_color_continuous当ggplot2检测到cyl是numeric向量后,会自动调用,为了直观,我们手动将这一函数写了出来。

在我们指定了颜色分箱之后,图例也发生了变化:

cont + scale_color_binned()

在这里插入图片描述
以下是连续的颜色标尺,(作用就不需要翻译了,很简单的英语,ggplot2官网也有这些函数详细的介绍):

连续的标尺函数颜色空间(调色板)显示的图例作用
scale_color_continuous()defaultColorbar默认
scale_color_gradient()user-definedColorbardefine low and high values
scale_color_gradient2()user-definedColorbardefine low mid and high values
scale_color_gradientn()user_definedColorbardefine any number of incremental val
scale_color_binned()defaultColorstepsbasic scale, but binned
scale_color_steps()user-definedColorstepsdefine low and high values
scale_color_steps2()user-definedColorstepsdefine low, mid, and high vals
scale_color_stepsn()user-definedColorstepsdefine any number of incremental vals
scale_color_viridis_c()ViridisColorbarviridis color scale. Change palette via option=.
scale_color_viridis_b()ViridisColorstepsViridis color scale, binned. Change palette via option=.
scale_color_distiller()BrewerColorbarBrewer color scales. Change palette via palette=.
scale_color_fermenter()BrewerColorstepsBrewer color scale, binned. Change palette via palette=.
# 离散标尺

当映射数据是离散的情况下,scale_color_discrete就会被调用,同样是上面的示例,我们用as.factor函数将数值向量强行转化为因子向量后,就可以得到离散的颜色标尺。

discrete <- ggplot(mtcars, aes(mpg, disp, color=as.factor(cyl))) + geom_point(size=4)
discrete# 在这里写不写scale_color_discrete没有区别,因为它是被默认调用的

在这里插入图片描述

如下是有关离散的函数,如英文原文示意:

标尺函数作用
scale_color_discrete()默认
scale_color_hue()Same as scale_color_discrete(), but you can define the range of hues and colors used
scale_color_grey()Uses a greyscale. Can define the range.
scale_color_manual()Must define specifically every color used. You can apply to your mapping by supplying a named vector for values=.
scale_color_identity()A special-case function where your data is made up of names of colors - not names of factor levels
scale_color_brewer()The discrete version of the Brewer colorspaces. Change palette via palette=.
scale_color_viridis_d()The discrete version of the viridis colorspaces. Can change palette via option=.

日期标尺

将日期映射到颜色我个人觉得是种很奇怪的行为(起码我一次都没遇到过),但既然ggplot2还是给了这种标尺(tidyverse就是喜欢重复造轮子),在文章的最后还是要介绍一下。
日期标尺主要分为scale_color_date()scale_color_datetime(), 顾名思义,但它们的本质和scale_color_continous没有区别,它们两都在定义函数的内部包装了scale_color_continous,使得在标签的显示上更令人易懂。

ggplot(economics, aes(x=date, y=unemploy, fill=date)) + geom_col() +scale_fill_date()

在这里插入图片描述

回顾曾经

如果你看到这里的话,那么下面的错误显然不用多加解释了。

> discrete + scale_color_continuous()
Error: Discrete value supplied to continuous scale

> cont + scale_color_discrete()
Error: Continuous value supplied to discrete scale
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
date; // 缴费日期 double amount; // 缴费金额 Payment(string n="", string d="", double a=0.0): name(n), date(d), amount(a) {} }; class PaymentManager { private: vector<Payment> payments;`scale_factor` 是像素与实际长度之间的比例因子,需要通过测量标尺的长度来计 // 缴费信息 public: // 添加缴费信息 void AddPayment(string n, string d, double a) { 算。可以按照以下步骤获取 `scale_factor`: 1. 读入含有标尺的图像并显示。 payments.push_back(Payment(n, d, a)); } // 获取缴费信息 Payment GetPayment(int i) { ```matlab img = imread('image_with_scale.png'); imshow(img); ``` 2. 在图像上使用 `imdistline return payments[i]; } // 获取缴费数量 int Count() { return payments.size(); } //` 函数绘制一个距离标尺,然后使用 `wait` 函数等待用户在图像上选择两个点来 修改缴费信息 void ModifyPayment(int i, string d, double a) { payments[i].date = d; payments设置标尺的长度。 ```matlab h = imdistline(gca); api = iptgetapi(h); api.setLabelTextFormatter('%[i].amount = a; } // 查询缴费信息 vector<Payment> QueryPayment(string n) { vector<d um'); wait(h); ``` 3. 获取标尺的长度,并计算出图像中每个像素的实际Payment> result; for (int i = 0; i < payments.size(); i++) { if (payments[i].name ==长度。 ```matlab distance = api.getDistance(); scale_factor = distance / 100; % 假设标尺长度为100像 n) { result.push_back(payments[i]); } } return result; } // 统计已交素 ``` 在这个例子中,我们假设标尺的长度为100像素,然后将标尺的和欠费用户 void Statistic(double deadline) { // 统计已交用户 vector<string> paid; 长度除以100得到每个像素的实际长度,从而得到了 `scale_factor`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值