您的位置:首页 > 其它

R语言中颜色使用方法参考汇总

2018-02-02 23:50 676 查看
ggplot2包中的颜色使用
载入数据

基本绘图

将变量的值映射到颜色

对色盲友好的调色板

色卡

The RColorBrewer package
Sequential palettes

Qualitative palettes

Diverging palettes

ggplot2包中的颜色使用

文章来源:cookbook-r

载入数据

# Two variables
df <- read.table(header=TRUE, text='
cond yval
A 2
B 2.5
C 1.6
')

# Three variables
df2 <- read.table(header=TRUE, text='
cond1 cond2 yval
A      I 2
A      J 2.5
A      K 1.6
B      I 2.2
B      J 2.4
B      K 1.2
C      I 1.7
C      J 2.3
C      K 1.9
')


基本绘图

library(ggplot2)
library(gridExtra)
# 默认的灰色条形图
p1 <- ggplot(df, aes(x=cond, y=yval)) + geom_bar(stat="identity")
# 设置轮廓颜色
p2 <- ggplot(df, aes(x=cond, y=yval)) + geom_bar(stat="identity", colour="#FF9999")
# 设置轮廓和内部颜色
p3 <- ggplot(df, aes(x=cond, y=yval)) + geom_bar(stat="identity", fill="#FF9999", colour="black")

# 黑色数据点和线
p4 <- ggplot(df, aes(x=cond, y=yval)) +
geom_line(aes(group=1)) +  # 注意这里的group组合数据,否则不会显示连线
geom_point(size=3)
# 深蓝色线条,红色数据点
p5 <- ggplot(df, aes(x=cond, y=yval)) +
geom_line(aes(group=1), colour="#000099") +  # 线
geom_point(size=3, colour="#CC0000")         # 点

grid.arrange(p1, p2, p3, p4, p5, ncol = 3)
g <- arrangeGrob(p1, p2, p3, p4, p5, ncol = 3)
ggsave("1.png", g)




将变量的值映射到颜色

将变量的值传入
aes()
中映射颜色

# 条形图的x轴和颜色根据cond2选择
p6 <- ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity")

# 另一组数据的条形图,颜色根据cond2选择
p7 <- ggplot(df2, aes(x=cond1, y=
4000
yval)) +
geom_bar(aes(fill=cond2),   # 颜色根据cond2选择
stat="identity",
colour="black",    # 给所有长条设置黑色边框
position=position_dodge()) # 设置为并排而不是堆叠

p8 <- ggplot(df2, aes(x=cond1, y=yval)) +
geom_line(aes(colour=cond2, group=cond2)) + # 线条颜色和分组都根据cond2选择
geom_point(aes(colour=cond2), size=3) # 数据点颜色根据cond2选择

grid.arrange(p6, p7, p8, ncol = 2)

g2 <- arrangeGrob(p6, p7, p8, ncol = 2)
ggsave("2.png", g2)




对色盲友好的调色板





可以将颜色参数存入变量中便于以后使用

# The palette with grey:
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

# The palette with black:
cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

# To use for fills, add
scale_fill_manual(values=cbPalette)

# To use for line and point colors, add
scale_colour_manual(values=cbPalette)




来源

色卡

十六进制颜色代码图

由RGB参数转换而来,前两个数字表示红色(R)的值,后四个数字依次代表绿色(G)和蓝色(B)的值。从00到FF是十六进制的范围,0到255是十进制的范围。例如,“#ffffff”是白色,“#990000”是深红色。



来源

The RColorBrewer package

文章来源:r-bloggers

Easily accessed with
scale_colour_brewer()
, it is trivial to choose among 35 palettes (see
RColorBrewer::display.brewer.all()
).

Sequential palettes

suited to ordered data that progress from low to high. Lightness steps dominate the look of these schemes, with light colors for low data values to dark colors for high data values.



Qualitative palettes

do not imply magnitude differences between legend classes, and hues are used to create the primary visual differences between classes. Qualitative schemes are best suited to representing nominal or categorical data.



Diverging palettes

put equal emphasis on mid-range critical values and extremes at both ends of the data range. The critical class or break in the middle of the legend is emphasized with light colors and low and high extremes are emphasized with dark colors that have contrasting hues.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: