R语言ggplot2作图好玩的配色:使用NBA各球队logo的配色

2022-05-23 15:47:04 浏览数 (1)

最近在学习R语言包的开发流程,看了之前分享过的关于吉普力工作室电影的配色包的代码,觉得里面的代码并不复杂,没准我也可以搞一个。模仿这个包的代码,我尝试把NBA各个球队logo配色整理成了R语言ggplot2包作图的时候可以使用的配色函数。

颜色来源于链接 https://usteamcolors.com/nba-colors/

首先将颜色整理成为一个yml格式的文件,基本文件格式如下

image.png

这里颜色最多的是太阳队,有7个可选,最少的是篮网对,只有一个黑色

定义函数

image.png

函数的代码

代码语言:javascript复制
nbalogo_palette <- function(name, n, direction = 1, type = c("discrete", "continuous")) {
  
  if (abs(direction) != 1) {
    stop("direction must be 1 or -1")
  }
  
  pal <- nba_logo[[name]]
  if (is.null(pal))
    stop("Palette not found.")
  
  if (missing(n)) {
    n <- length(pal)
  }
  
  type <- match.arg(type)
  
  if (type == "discrete" && n > length(pal)) {
    stop(paste0("Number of requested colors greater than what palette can offer, which is ",
                length(pal), "."))
  }
  
  out <- switch(type,
                continuous = colorRampPalette(pal)(n),
                discrete = pal[1:n]
  )
  
  if (direction == -1) {
    out <- rev(out)
  }
  
  structure(out, class = "palette", name = name)
}


pal_pal <- function(name, direction) {
  
  function(n) {
    nbalogo_palette(name = name, direction = direction)
  }
  
}

scale_colour_nbalogo_c<-function(name,direction=1,...){
  
  if(!requireNamespace("ggplot2",quietly = TRUE)){
    stop('ggplot2 is required for this functionality',call. = FALSE)
  }
  
  ggplot2::scale_colour_gradientn(
    colours = nbalogo_palette(name = name,direction = direction,
                              n = 256, type = "continuous"),...
  )
}

scale_color_nbalogo_c <- scale_colour_nbalogo_c
scale_fill_nbalogo_d <- function(name, direction = 1, ...) {
  
  if (!requireNamespace("ggplot2", quietly = TRUE)) {
    stop('ggplot2 is required for this functionality', call. = FALSE)
  }
  
  ggplot2::discrete_scale("fill", scale_name = name,
                          pal_pal(name = name, direction = direction), ...)
  
}

scale_fill_nbalogo_c<-function(name,direction=1){
  if (!requireNamespace("ggplot2", quietly = TRUE)) {
    stop('ggplot2 is required for this functionality', call. = FALSE)
  }
  
  ggplot2::scale_fill_gradientn(
    colours = nbalogo_palette(name = name, direction = direction,
                              n = 256, type = "continuous"), ...)
}
scale_colour_nbalogo_d<-function(name,direction=1,...){
  
  if(!requireNamespace("ggplot2",quietly=TRUE)){
    stop("ggplot2 is required for this functionality",call. = FALSE)
  }
  ggplot2::discrete_scale("colour",scale_name = name,
                          pal_pal(name = name,direction = direction),...)
}

scale_color_nbalogo_d<-scale_colour_nbalogo_d

使用这些颜色

首先是读取颜色

代码语言:javascript复制
nba_logo <- yaml::read_yaml(file = "../Rpackagedev/ggNBAlogo/nba_logo.yml")
nba_logo

展示一下都有哪些颜色

代码语言:javascript复制
lapply(nba_logo, prismatic::colour)

image.png

加载自定义的函数

代码语言:javascript复制
x<-list.files("../Rpackagedev/ggNBAlogo/",pattern = "*.R",full.names = T)

lapply(x,source)

接下来使用太阳队和雷霆队的配色来举个例子

代码语言:javascript复制
library(ggplot2)

library(ggimg)

dat01<-data.frame(x=letters[1:7],
                y=1:7)
ggplot(data=dat01,aes(x=x,y=y)) 
  geom_col(aes(fill=x),show.legend = F) 
  scale_fill_nbalogo_d(name='Phoenix_Suns') 
  theme_void() 
  geom_point_img(aes(x=2,y=6),
                 img="../Rpackagedev/ggNBAlogo/phoenix-suns.png",
                 size=8) ->p1

dat02<-data.frame(x=letters[1:4],
                  y=c(1,1,1,6))
ggplot(data=dat02,aes(x=x,y=y)) 
  geom_col(aes(fill=x),show.legend = F) 
  scale_fill_nbalogo_d(name='Oklahoma_CityThunder') 
  theme_void() 
  geom_point_img(aes(x=2,y=4),
                 img="../Rpackagedev/ggNBAlogo/oklahoma-city-thunder.png",
                 size=8) ->p2
library(patchwork)
p1 p2

image.png

0 人点赞