打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ggplot2 colors : How to change colors automatically and manually?

The goal of this article is to describe how to change the color of a graph generated using R software and ggplot2 package. A color can be specified either by name (e.g.: “red”) or by hexadecimal code (e.g. : “#FF1234”). The different color systems available in R are described at this link : colors in R.

本文的目标是描述如何更改使用 r 软件和 ggplot2包生成的图的颜色。 可以通过名称(例如: “ red”)或十六进制代码(例如: “ # ff1234”)指定颜色。 在 r 中可用的不同颜色系统描述在这个链接: r 中的颜色。

In this R tutorial, you will learn how to :

在这个 r 教程中,你将学习如何:

  • change colors by groups (automatically and manually) 按组更改颜色(自动和手动)
  • use RColorBrewer and Wes Anderson color palettes 使用 RColorBrewer 和 Wes Anderson 的调色板
  • use gradient colors 使用渐变颜色

Prepare the data

准备数据

ToothGrowth and mtcars data sets are used in the examples below.

下面的例子使用了 ToothGrowth 和 mtcars 数据集。

# Convert dose and cyl columns from numeric to factor variablesToothGrowth$dose <- as.factor(ToothGrowth$dose)mtcars$cyl <- as.factor(mtcars$cyl)head(ToothGrowth)
##    len supp dose## 1  4.2   VC  0.5## 2 11.5   VC  0.5## 3  7.3   VC  0.5## 4  5.8   VC  0.5## 5  6.4   VC  0.5## 6 10.0   VC  0.5
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Make sure that the columns dose and cyl are converted as factor variables using the R script above.

确保使用上面的 r 脚本将列剂量和 cyl 转换为因子变量。

Simple plots

简单的情节

library(ggplot2)# Box plotggplot(ToothGrowth, aes(x=dose, y=len)) +geom_boxplot()# scatter plotggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()

Use a single color

使用单一的颜色

# box plotggplot(ToothGrowth, aes(x=dose, y=len)) +  geom_boxplot(fill='#A4A4A4', color="darkred")# scatter plotggplot(mtcars, aes(x=wt, y=mpg)) +   geom_point(color='darkblue')

Change colors by groups

按组改变颜色

Default colors

默认颜色

The following R code changes the color of the graph by the levels of dose :

下面的 r 代码根据剂量水平改变图表的颜色:

# Box plotbp<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +  geom_boxplot()bp# Scatter plotsp<-ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) + geom_point()sp

The lightness (l) and the chroma (c, intensity of color) of the default (hue) colors can be modified using the functions scale_hue as follow :

默认颜色(色调)的亮度(l)和彩度(c,颜色的强度)可以通过以下函数调整色调:

# Box plotbp + scale_fill_hue(l=40, c=35)# Scatter plotsp + scale_color_hue(l=40, c=35)

Note that, the default values for l and c are : l = 65, c = 100.

注意,l 和 c 的默认值是: l 65,c 100。

Change colors manually

手动更改颜色

A custom color palettes can be specified using the functions :

可以使用以下函数指定自定义调色板:

  • scale_fill_manual() for box plot, bar plot, violin plot, etc 规模填充说明书()用于箱形地块、条形地块、小提琴地块等
  • scale_color_manual() for lines and points 线和点的刻度颜色说明书()
# Box plotbp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))# Scatter plotsp + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))

Note that, the argument breaks can be used to control the appearance of the legend. This holds true also for the other scale_xx() functions.

注意,可以使用参数中断来控制图例的外观。 对于其他的比例尺 xx ()函数也是如此。

# Box plotbp + scale_fill_manual(breaks = c("2", "1", "0.5"),                        values=c("red", "blue", "green"))# Scatter plotsp + scale_color_manual(breaks = c("8", "6", "4"),                        values=c("red", "blue", "green"))

The built-in color names and a color code chart are described here : color in R.

描述了内置的颜色名称和颜色代码图: r 中的颜色。

Use RColorBrewer palettes

使用 RColorBrewer 调色板

The color palettes available in the RColorBrewer package are described here : color in R.

在 RColorBrewer 包中可用的调色板描述如下:。

# Box plotbp + scale_fill_brewer(palette="Dark2")# Scatter plotsp + scale_color_brewer(palette="Dark2")

The available color palettes in the RColorBrewer package are :

在 RColorBrewer 包中可用的调色板有:

Use Wes Anderson color palettes

使用韦斯 · 安德森的调色板

Install and load the color palettes as follow :

安装并加载调色板,如下所示:

# Installinstall.packages("wesanderson")# Loadlibrary(wesanderson)

The available color palettes are :

现有的调色板有:

library(wesanderson)# Box plotbp+scale_fill_manual(values=wes_palette(n=3, name="GrandBudapest"))# Scatter plotsp+scale_color_manual(values=wes_palette(n=3, name="GrandBudapest"))

Use gray colors

使用灰色

The functions to use are :

使用的功能如下:

  • scale_colour_grey() for points, lines, etc 用灰色标出点、线等
  • scale_fill_grey() for box plot, bar plot, violin plot, etc 方块、条块、小提琴块等的灰度填充()
# Box plotbp + scale_fill_grey() + theme_classic()# Scatter plotsp + scale_color_grey() + theme_classic()

Change the gray value at the low and the high ends of the palette :

改变调色板高低两端的灰度值:

# Box plotbp + scale_fill_grey(start=0.8, end=0.2) + theme_classic()# Scatter plotsp + scale_color_grey(start=0.8, end=0.2) + theme_classic()

Note that, the default value for the arguments start and end are : start = 0.2, end = 0.8

请注意,start 和 end 参数的默认值是: start 0.2,end 0.8

Continuous colors

连续的颜色

The graph can be colored according to the values of a continuous variable using the functions :

可以根据连续变量的值使用以下函数对图形进行着色:

  • scale_color_gradient(), scale_fill_gradient() for sequential gradients between two colors 缩放颜色渐变() ,两种颜色之间顺序渐变的缩放填充渐变()
  • scale_color_gradient2(), scale_fill_gradient2() for diverging gradients 标度颜色梯度2() ,用于发散梯度的标度填充梯度2()
  • scale_color_gradientn(), scale_fill_gradientn() for gradient between n colors 刻度颜色梯度 n () ,刻度填充梯度 n ()之间的梯度

Gradient colors for scatter plots

散点图的渐变颜色

The graphs are colored using the qsec continuous variable :

这些图使用 qsec 连续变量着色:

# Color by qsec valuessp2<-ggplot(mtcars, aes(x=wt, y=mpg, color=qsec)) + geom_point()sp2# Change the low and high colors# Sequential color schemesp2+scale_color_gradient(low="blue", high="red")# Diverging color schememid<-mean(mtcars$qsec)sp2+scale_color_gradient2(midpoint=mid, low="blue", mid="white",                     high="red", space ="Lab" )

Gradient colors for histogram plots

直方图的渐变颜色

set.seed(1234)x <- rnorm(200)# Histogramhp<-qplot(x =x, fill=..count.., geom="histogram") hp# Sequential color schemehp+scale_fill_gradient(low="blue", high="red")

Note that, the functions scale_color_continuous() and scale_fill_continuous() can be used also to set gradient colors.

注意,缩放函数的颜色连续()和缩放填充连续()也可以用来设置渐变颜色。

Gradient between n colors

N 个颜色之间的渐变

# Scatter plot# Color points by the mpg variablesp3<-ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) + geom_point()sp3# Gradient between n colorssp3+scale_color_gradientn(colours = rainbow(5))

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ggplot2学习笔记之颜色标度
scale相关设置
CorelDRAW颜色填充
(2)Matlab中的color 画线的多种颜色
R语言学习ggplot2之颜色设置,你学会了吗?
文献配套GitHub发表级别绘图03-条形图
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服