Programming

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

procodes 2020. 7. 15. 22:19
반응형

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


범주 형 데이터 집합을 플로팅하고 다른 범주를 나타내는 데 고유 한 색을 사용하려고합니다. 숫자가 주어지면 R에서 가장 많은 고유 색상을 n어떻게 얻을 n수 있습니까? 감사.


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)]

20 색 세트

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

200의 색깔로 n = 200:

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

200 색 세트


몇 가지 옵션이 있습니다.

  1. 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. colorBrewer패키지 및 웹 사이트를 보십시오 . 당신은 색상을 발산하려면, 선택한 다음 분기 사이트. 예를 들어

    library(colorBrewer)
    brewer.pal(7, "BrBG")
    
  4. 내가 원하는 색상 웹 사이트는 좋은 팔레트를 많이 제공합니다. 다시, 필요한 팔레트를 선택하십시오. 예를 들어, 사이트에서 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)))
    

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

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

원형 차트에서 시각화 할 때 매우 다른 색상 세트가 선택되었음을 알 수 있습니다 (여기에서 다른 답변에서 제안한 바와 같이).

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

여기에 이미지 설명을 입력하십시오

50 가지 색상의 원형 차트에 표시 :

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

여기에 이미지 설명을 입력하십시오


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

팔레트를 얻으려면 패키지를 설치하고 함수를 사용하십시오 viridis_pal(). 선택할 수있는 "A", "B", "C"및 "D"의 네 가지 옵션이 있습니다.

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

여기에 이미지 설명을 입력하십시오

여기에 이미지 설명을 입력하십시오

여기에 이미지 설명을 입력하십시오

YouTube에서 좋은 컬러 ​​맵의 복잡성을 설명하는 훌륭한 연설도 있습니다.

Matplotlib를위한 더 나은 기본 컬러 맵 | 사이 파이 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/

has a service to compose any size of palette according to various parameters and

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

discusses the generic problem from a graphics designers perspective and gives lots of examples of usable palettes.

To comprise a palette from RGB values you just have to copy the values in a vector as in e.g.:

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")

I found a website offering a list of 20 distinctive colours: 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')

You can have a try!


You can generate a set of colors like this:

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")

These colors are as distinct as possible. For those similar colors, they form a gradient so that you can easily tell the differences between them.

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

반응형