R绘图_ggplot2图形参数之配色[3]

火狐截图_2020-02-11T08-36-22.554Z.png

ggplot2 colors : How to change colors automatically and manually?

ggplot2 :如何自动和手动更改颜色?

本教程的目的是描述如何更改使用R和ggplot2包生成的图的颜色。

(一):数据准备

rm(list = ls())
#convert dose and cyl columns from a numeric to a factor variables
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
mtcars$cyl <- as.factor(mtcars$cyl)
head(ToothGrowth)
head(mtcars)

(二):单色绘图

library(ggplot2)
library(ggpubr)
# Box plot
a <- ggplot(ToothGrowth, aes(x=dose, y=len)) +geom_boxplot()
# scatter plot
b <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
# box plot
c <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
  geom_boxplot(fill='#A4A4A4', color="darkred")
# scatter plot
d <- ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point(color='darkblue')
ggarrange(a,b,c, d,
          labels = c("A","B","C","D"),
          nrow = 2, ncol = 2)
Rplot01.png

(三):按组更改颜色

3.1. 默认配色
#Change colors by groups
# Box plot
bp <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_boxplot()
# Scatter plot
sp <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) + geom_point()
# Box plot
bp1 <- bp + scale_fill_hue(l=40, c=35)
# Scatter plot
sp1 <- sp + scale_color_hue(l=40, c=35)
ggarrange(bp, sp,bp1, sp1,
          labels = c("A","B","C","D"),
          nrow = 2, ncol = 2)

scale_color_gradient():用于两种颜色之间的顺序渐变
scale_color_gradient2():用于分散梯度
scale_color_gradientn():用于n种颜色之间的渐变

Rplot02.png
3.2. 手动更改配色
#Change colors manually
# Box plot
m <- bp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Scatter plot
n <- sp + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Box plot
o <- bp + scale_fill_manual(breaks = c("2", "1", "0.5"), 
                       values=c("red", "blue", "green"))
# Scatter plot
p <- sp + scale_color_manual(breaks = c("8", "6", "4"),
                        values=c("red", "blue", "green"))
ggarrange(m, n, o, p, 
          labels = c("A","B","C","D"),
          nrow = 2, ncol = 2)
Rplot03.png
3.3. RColorBrewer设置颜色
#Use RColorBrewer palettes
# Box plot
x <- bp + scale_fill_brewer(palette="Dark2")
# Scatter plot
y <- sp + scale_color_brewer(palette="Dark2")
ggarrange(x, y,
          labels = c("A","B"),
          nrow = 1)
Rplot04.png
3.4. Wes Anderson设置颜色
#Use Wes Anderson color palettes
library(wesanderson)
# Box plot
x1 <- bp+scale_fill_manual(values=wes_palette(n=3, name="GrandBudapest1"))
# Scatter plot
x2 <- sp+scale_color_manual(values=wes_palette(name="GrandBudapest1"))
ggarrange(x1, x2,
          labels = c("A","B"),
          nrow = 1)
Rplot05.png

(四): 设置灰色

#Use gray colors
# Box plot
g1 <- bp + scale_fill_grey() + theme_classic()
# Scatter plot
g2 <- sp + scale_color_grey() + theme_classic()
# Box plot
g3 <- bp + scale_fill_grey(start=0.8, end=0.2) + theme_classic()
# Scatter plot
g4 <- sp + scale_color_grey(start=0.8, end=0.2) + theme_classic()
ggarrange(g1,g2,g3,g4, 
          labels = c("A","B","C","D"),
          nrow = 2, ncol = 2)
Rplot06.png

(五): 设置连续色

5.1. 散点图的渐变颜色

#Continuous colors
# Color by qsec values
sp2<-ggplot(mtcars, aes(x=wt, y=mpg, color=qsec)) + geom_point()

# Change the low and high colors
# Sequential color scheme
sp3 <- sp2+scale_color_gradient(low="blue", high="red")
# Diverging color scheme 不同的配色方案
mid<-mean(mtcars$qsec)
sp4 <- sp2+scale_color_gradient2(midpoint=mid, low="blue", mid="white",
                          high="red", space ="Lab" )
ggarrange(sp2,sp3,sp4, 
          labels = c("A","B","C"),
          nrow = 1, ncol = 3)
Rplot07.png

5.2. 直方图的渐变颜色

#Gradient colors for histogram plots
set.seed(1234)
x <- rnorm(200)
# Histogram
hp <- qplot(x =x, fill=..count.., geom="histogram") 
# Sequential color scheme
hp1 <- hp+scale_fill_gradient(low="blue", high="red")

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

ggarrange(hp,hp1,sp3,sp4, 
          labels = c("A","B","C","D"),
          nrow = 2, ncol = 2)
Rplot08.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容