programing

R에서 가장 독특한 색상을 생성하는 방법은 무엇입니까?

itmemos 2023. 6. 16. 21:27
반응형

R에서 가장 독특한 색상을 생성하는 방법은 무엇입니까?

범주형 데이터 집합을 표시하고 있으며 다른 범주를 나타내기 위해 고유한 색상을 사용하려고 합니다. 호번지가 주어집니다.n어떻게 해야 합니까?nR의 MOST 구별되는 색상의 수는?감사해요.

나는 모든 질적인 팔레트에 가입했습니다.RColorBrewer는 각각 있습니다.정성 팔레트는 각각 X개의 가장 독특한 색상을 제공해야 합니다.물론, 그것들을 섞는 것은 하나의 팔레트에 또한 비슷한 색상으로 결합하지만, 그것이 제가 얻을 수 있는 최고의 색상입니다(74가지 색상).

library(RColorBrewer)
n <- 60
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
pie(rep(1,n), col=sample(col_vector, n))

colour_Brewer_qual_60

다른 해결책은: 그래픽 장치에서 모든 R 색상을 가져와서 샘플링하는 것입니다.회색 음영이 너무 비슷해서 제거했습니다.이것은 433가지 색상을 제공합니다.

color = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)]

set of 20 colours

pie(rep(1,n), col=sample(color, n))

200상의로n = 200:

pie(rep(1,n), col=sample(color, n))

set of 200 colours

다음은 몇 가지 옵션입니다.

  1. ▁a▁를 보세요.palette함수:

     palette(rainbow(6))     # six color rainbow
     (palette(gray(seq(0,.9,len = 25)))) #grey scale
    
  2. 리고그.colorRampPalette함수:

     ##Move from blue to red in four colours
     colorRampPalette(c("blue", "red"))( 4) 
    
  3. 패키지( 웹 사이트)를 확인합니다.다양한 색상을 원하는 경우 사이트에서 다양한 색상을 선택합니다.예를들면,

     library(RColorBrewer)
     brewer.pal(7, "BrBG")
    
  4. I want hue 웹사이트는 멋진 팔레트를 많이 제공합니다.다시 한 번, 필요한 팔레트를 선택하십시오.예를 들어 사이트에서 RGB 색상을 가져와 자신만의 팔레트를 만들 수 있습니다.

     palette(c(rgb(170,93,152, maxColorValue=255),
         rgb(103,143,57, maxColorValue=255),
         rgb(196,95,46, maxColorValue=255),
         rgb(79,134,165, maxColorValue=255),
         rgb(205,71,103, maxColorValue=255),
         rgb(203,77,202, maxColorValue=255),
         rgb(115,113,206, maxColorValue=255)))
    

패키지를 사용해 볼 수도 있습니다.

library(randomcoloR)
n <- 20
palette <- distinctColorPalette(n)

파이 차트에서 시각화할 때 매우 구별되는 색상 집합이 선택되는 것을 확인할 수 있습니다(여기에 있는 다른 답변에서 제안됨).

pie(rep(1, n), col=palette)

enter image description here

파이 차트에 50가지 색상으로 표시:

n <- 50
palette <- distinctColorPalette(n)
pie(rep(1, n), col=palette)

enter image description here

OP의 질문에 대한 답변은 아니지만 순차적 데이터를 위한 좋은 색상 팔레트가 있는 패키지가 있다는 것은 언급할 가치가 있습니다.이 제품들은 인식적으로 균일하고 색맹이 안전하며 프린터 친화적입니다.

.viridis_pal()" " " "를네 가지 옵션이 .

install.packages("viridis")
library(viridis)
viridis_pal(option = "D")(n)  # n = number of colors seeked

enter image description here

enter image description here

enter image description here

유튜브에는 좋은 컬러맵의 복잡성을 설명하는 훌륭한 강연도 있습니다.

Matplotlib을 위한 더 나은 기본 색상 지도 | SciPy 2015 | 나타니엘 스미스와 스테판 판 데어 월트.

사용할 수 있습니다.colorRampPalette 또는 베스또는에서RColorBrewer패키지:

와 함께colorRampPalette다음과 같이 색상을 지정할 수 있습니다.

colorRampPalette(c("red", "green"))(5)
# [1] "#FF0000" "#BF3F00" "#7F7F00" "#3FBF00" "#00FF00"

또는 16진수 코드를 제공할 수 있습니다.

colorRampPalette(c("#3794bf", "#FFFFFF", "#df8640"))(5)
# [1] "#3794BF" "#9BC9DF" "#FFFFFF" "#EFC29F" "#DF8640"
# Note that the mid color is the mid value...

와 함께RColorBrewer기존 팔레트의 색상을 사용할 수 있습니다.

require(RColorBrewer)
brewer.pal(9, "Set1")
# [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00" "#FFFF33" "#A65628" "#F781BF"
# [9] "#999999"

를 .RColorBrewer다른 팔레트용 패키지입니다.이게 도움이 되길 바랍니다.

대형 컬러 팔레트는 외부 소스를 사용하는 것이 좋습니다.

http://tools.medialab.sciences-po.fr/iwanthue/

다양한 매개변수에 따라 임의의 크기의 팔레트를 구성하는 서비스가 있습니다.

https://graphicdesign.stackexchange.com/questions/3682/where-can-i-find-a-large-palette-set-of-contrasting-colors-for-coloring-many-d/3815

그래픽 디자이너의 관점에서 일반적인 문제를 설명하고 사용 가능한 팔레트의 예를 많이 제공합니다.

RGB 값에서 팔레트를 구성하려면 다음과 같이 벡터에 값을 복사하면 됩니다.

colors37 = c("#466791","#60bf37","#953ada","#4fbe6c","#ce49d3","#a7b43d","#5a51dc","#d49f36","#552095","#507f2d","#db37aa","#84b67c","#a06fda","#df462a","#5b83db","#c76c2d","#4f49a3","#82702d","#dd6bbb","#334c22","#d83979","#55baad","#dc4555","#62aad3","#8c3025","#417d61","#862977","#bba672","#403367","#da8a6d","#a79cd4","#71482c","#c689d0","#6b2940","#d593a7","#895c8b","#bd5975")

는 20가지 색상의 목록을 제공하는 웹사이트를 찾았습니다: https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/

col_vector<-c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080', '#ffffff', '#000000')

한 번 해보세요!

이 용도로 Polychrome 패키지를 사용할 수 있습니다.색상 수와 몇 가지만 있으면 됩니다.seedcolors예:

# install.packages("Polychrome")
library(Polychrome)

# create your own color palette based on `seedcolors`
P36 = createPalette(36,  c("#ff0000", "#00ff00", "#0000ff"))
swatch(P36)

패키지에 대한 자세한 내용은 https://www.jstatsoft.org/article/view/v090c01 에서 확인할 수 있습니다.

제가 알기로는 독특한 색상을 검색하는 것은 단위 큐브에서 효율적으로 검색하는 것과 관련이 있습니다. 여기서 큐브의 3차원은 빨간색, 녹색 및 파란색 축을 따라 3개의 벡터입니다.이는 실린더(HSV 유추)에서 검색을 단순화할 수 있으며, 여기서 포화도(S) 및 값(V)을 수정하고 임의의 색상 값을 찾을 수 있습니다.대부분의 경우 작동하며, 여기에서 이를 확인하십시오.

https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/

R에서,

get_distinct_hues <- function(ncolor,s=0.5,v=0.95,seed=40) {
  golden_ratio_conjugate <- 0.618033988749895
  set.seed(seed)
  h <- runif(1)
  H <- vector("numeric",ncolor)
  for(i in seq_len(ncolor)) {
    h <- (h + golden_ratio_conjugate) %% 1
    H[i] <- h
  }
  hsv(H,s=s,v=v)
}

다른 방법은 R 패키지 "sublish" https://cran.r-project.org/web/packages/uniformly/index.html 을 사용하는 것입니다.

그리고 이 간단한 기능은 독특한 색상을 생성할 수 있습니다.

get_random_distinct_colors <- function(ncolor,seed = 100) {
  require(uniformly)
  set.seed(seed)
  rgb_mat <- runif_in_cube(n=ncolor,d=3,O=rep(0.5,3),r=0.5)
  rgb(r=rgb_mat[,1],g=rgb_mat[,2],b=rgb_mat[,3])
}

그리드 검색을 통해 조금 더 관련된 기능을 생각할 수 있습니다.

get_random_grid_colors <- function(ncolor,seed = 100) {
  require(uniformly)
  set.seed(seed)
  ngrid <- ceiling(ncolor^(1/3))
  x <- seq(0,1,length=ngrid+1)[1:ngrid]
  dx <- (x[2] - x[1])/2
  x <- x + dx
  origins <- expand.grid(x,x,x)
  nbox <- nrow(origins) 
  RGB <- vector("numeric",nbox)
  for(i in seq_len(nbox)) {
    rgb <- runif_in_cube(n=1,d=3,O=as.numeric(origins[i,]),r=dx)
    RGB[i] <- rgb(rgb[1,1],rgb[1,2],rgb[1,3])
  }
  index <- sample(seq(1,nbox),ncolor)
  RGB[index]
} 

다음을 통해 이 기능을 확인합니다.

ncolor <- 20
barplot(rep(1,ncolor),col=get_distinct_hues(ncolor))          # approach 1
barplot(rep(1,ncolor),col=get_random_distinct_colors(ncolor)) # approach 2
barplot(rep(1,ncolor),col=get_random_grid_colors(ncolor))     # approach 3

그러나 사람이 인식할 수 있는 색상으로 구별되는 팔레트를 정의하는 것은 간단하지 않습니다.위의 방법 중 어떤 것이 다양한 색상 세트를 생성하는지는 아직 테스트되지 않았습니다.

다음과 같은 색상 집합을 생성할 수 있습니다.

myCol = c("pink1", "violet", "mediumpurple1", "slateblue1", "purple", "purple3",
          "turquoise2", "skyblue", "steelblue", "blue2", "navyblue",
          "orange", "tomato", "coral2", "palevioletred", "violetred", "red2",
          "springgreen2", "yellowgreen", "palegreen4",
          "wheat2", "tan", "tan2", "tan3", "brown",
          "grey70", "grey50", "grey30")

이 색깔들은 가능한 한 뚜렷합니다.유사한 색상의 경우 그라데이션을 형성하여 쉽게 차이를 구분할 수 있습니다.

저는 다음과 같이 가장 구별할 수 있는 8가지 색상을 수작업으로 찾았습니다.

color = c("blue", red", green3", hotpink3", yellow", maroon1", lightsalmon3", black")

하지만 더 필요하시면 여기를 보시고 직접 목록을 만드세요: http://www.stat.columbia.edu/ ~tzheng/files/Rcolor.pdf

언급URL : https://stackoverflow.com/questions/15282580/how-to-generate-a-number-of-most-distinctive-colors-in-r

반응형