美文网首页ggplot2绘图
ggplot 颜色/透明/线条/形状标度系统

ggplot 颜色/透明/线条/形状标度系统

作者: drlee_fc74 | 来源:发表于2019-04-05 19:55 被阅读140次

透明标度

library(ggplot2)
diamonds1 <- diamonds[sample(nrow(diamonds),1000),]
##基本作图
p <- ggplot(diamonds1, aes(depth, price)) +
  geom_point(aes(alpha = carat))
##更改透明标签的名字
p1 <- p + scale_alpha("new_legend")
##更改连续性透明的范围
p2 <- p + scale_alpha_continuous(range = c(0.4,0.8))
##离散型透明范围改变
p3 <- ggplot(diamonds1, aes(depth, price)) +
  geom_point(aes(alpha = color)) + scale_alpha_manual("cccolr",
  limits = levels(diamonds$color),              
  breaks =  c("D","E","F","G","H"),
  labels = c("D-d","E-e","F-f","G-g","H-h"),
  values = c(0.15,0.2,0.3,0.4,0.5,0.6,0.75)
  )
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggplot2':
## 
##     ggsave
plot_grid(p,p1,p2,p3, ncol = 2)

颜色标度

颜色分为填充色(fill)和轮廓色(color)。所有的颜色都可以使用这两个来定义颜色 函数的常规参数 #### ColorBrewer配色系统 在介绍颜色标度之前,先介绍一个成熟的系统的颜色配色。这样好选择好看的颜色

library(RColorBrewer)
##展示所有的配色方案
display.brewer.all()

[图片上传中...(image-fe81f4-1554464628287-5)]

连续性颜色标度

mydata3 <- data.frame(
  x = runif(100,0,100),
  y = runif(100,0,100),
  z = runif(100,0,100),
  f = runif(100,0,100),
  g = rep(LETTERS[1:5],each = 20)
)
p <- ggplot(mydata3, aes(x,y,color = z), shape = 19, size = 5) + geom_point()
##双色过渡渐变 scale_color_continuous() == scale_color_gradient()
p1 <- p + scale_color_continuous(name = "colors",##图例的名字
                                 limits = c(0.5,150),##定义颜色分配范围
                                 breaks = c(1,20,50,80), ##制定颜色的分配
                                 labels = paste0(c(1,20,50,80), "G"), ##break的标签。但是长度要和breaks一样。
                                 low = "blue", high = "red"
                                 ##na.value = "grey", ##定义缺失值的颜色
                                 )
###三色过渡渐变
##三色渐变除了上面两色的参数就是多了一个颜色和中位颜色的设定
p2 <- p + scale_color_gradient2(low = "blue", mid = "black", high = "red", midpoint = median(mydata3$z)) ##函数提供了midpoint参数用来设置中点
###成熟的渐变颜色变化
p3 <- p + scale_color_distiller("distiller", ###改变图例的名字
                                type = "div", ##颜色使用的类型seq (连续性), div (发散性) or qual (定性)。这个可以不定义
                                palette = "RdYlGn", ##定义具体颜色配色
                                direction = -1, ##配色方案选择颜色的顺序,1位正向,-1位负向
                                values = c(0.2, 0.8)##制定颜色匹配的范围。例子中的代表整个数字的20%-80%进行标注颜色
                                )
plot_grid(p1,p2,p3, ncol = 3)

[图片上传中...(image-909c2d-1554464628286-1)]

离散型颜色标度

离散型标度一般用scale_color_discrete,由于定义颜色比较麻烦。用的少。一般离散型的都是用自己自定义颜色或者成熟的配色方案

p <- ggplot(mydata3, aes(x,y, color = g), shape = 19, size = 5) + geom_point()
p1 <- p + scale_color_discrete(name = "discrete",
                               limits = c("A","C","D","E"), ##定义颜色分类
                               breaks = c("A","D","E"),
                               labels = paste0(c("A","D","E"),"-W"),
                               l = 40, c = 30)
##成熟的配色方案
p2 <- p + scale_color_brewer(type = "seq", palette = 1, direction = -1)
p3 <- p + scale_colour_brewer(palette = "RdYlBu", direction = -1)
p4 <- p + scale_colour_brewer(palette = "Accent", direction = -1)
###自定义色盘。
p5 <- p + scale_colour_manual(
  limits = LETTERS[1:4],
  breaks = LETTERS[4:1],
  labels = c("A_a","B_b","C_c","D_d"),
  values = c("#F46D43","#D9F0D3","#1A9850","#006837")
)
plot_grid(p,p1,p2,p3,p4,p5, ncol = 2, nrow = 3)
## Warning: Removed 20 rows containing missing values (geom_point).

[图片上传中...(image-91e113-1554464628286-0)]

线和形状标度标度

ggplot里面有六种线条类型,其中分别为0=blank,1=solid,2=dashed,3=dotted,4=dotdash,5=longdash,6=twodash 一般而言linetype和shape都是自己手动自定的或者是离散的。没有连续性的线条

lct <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
## [1] "C"
mydata4 <- data.frame(
  x = rep(seq(as.Date('2018-03-01'),as.Date('2018-03-10'),by = '1 day'),each = 5),
  y = abs(rnorm(50,1000,500)),
  z = rep(LETTERS[1:5],length = 50)
)
p <- ggplot(mydata4,aes(x,y)) +
  geom_line(aes(linetype = z,colour = z),size = 1.5)
p1 <- p + scale_linetype_manual(
  limits = LETTERS[1:5],
  breaks = LETTERS[1:5],
  labels = paste0(LETTERS[1:5],'_',letters[1:5]),
  values = c('solid','dashed','dotted','dotdash','longdash')
  )
plot_grid(p,p1,ncol = 1)

[图片上传中...(image-568667-1554464628287-4)]

##形状标度
p <- ggplot(mydata4,aes(x,y,colour = z)) +
  geom_line(size = 1.5) +
  geom_point(aes(shape = z),size =  5)
p2 <- p +scale_shape_manual(
  limits = LETTERS[1:5],
  breaks = LETTERS[1:5],
  labels = paste0(LETTERS[1:5],'_',letters[1:5]),
  values = c(0,1,2,5,6)
 )
plot_grid(p,p2,ncol = 1)

[图片上传中...(image-e4a154-1554464628287-3)]

点的大小

mydata6 <- data.frame(
  x = runif(100,1,100),
  y = runif(100,1,100),
  z = runif(100,1,100)
)
p <- ggplot(mydata6,aes(x,y,size = z)) +
  geom_point()
p1 <- p + scale_radius()
##自定义大小
p2 <- p + scale_size_area(
  limits = c(20,80),
  breaks = seq(20,80,5),
  labels = paste(seq(20,80,5),'$'))
plot_grid(p,p1,p2,ncol= 1)
## Warning: Removed 35 rows containing missing values (geom_point).
image.png

相关文章

网友评论

    本文标题:ggplot 颜色/透明/线条/形状标度系统

    本文链接:https://www.haomeiwen.com/subject/mcukiqtx.html