我可以使用什麼函數來模擬ggplot2的默認調色板以獲得所需數量的顏色。例如,爲3的輸入將產生的HEX顏色的字符向量與這些顏色: 仿真ggplot2默認調色板
121
A
回答
167
它僅僅是等距隔開的色調在色輪上,從15開始:
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
例如:
n = 4
cols = gg_color_hue(n)
dev.new(width = 4, height = 4)
plot(1:n, pch = 16, cex = 2, col = cols)
35
從哈德利韋翰的GGPLOT2書的106頁:
默認配色方案scale_colour_hue在hcl色輪周圍採用均勻分佈的色調 。
有了一點逆向工程,你可以構建這樣的功能:
ggplotColours <- function(n = 6, h = c(0, 360) + 15){
if ((diff(h) %% 360) < 1) h[2] <- h[2] - 360/n
hcl(h = (seq(h[1], h[2], length = n)), c = 100, l = 65)
}
在barplot演示這一點:
y <- 1:3
barplot(y, col = ggplotColours(n = 3))
+2
它比這更簡單。你可以避免代數的第一行,因爲雖然它不在幫助中,但是「hcl」可以循環使用大於360的值。 –
+10
甚至可以使用'scales ::: show_col(ggplotColours(n = 3))'來顯示顏色和值 –
41
這些答案都很好,但我想分享另一件我在stackoverflow上發現的東西,這真的很有用,這裏是t他direct link
基本上,@DidzisElferts展示瞭如何獲取ggplot用於構建您創建的繪圖的所有顏色,座標等。非常好!
p <- ggplot(mpg,aes(x=class,fill=class)) + geom_bar()
ggplot_build(p)$data
[[1]]
fill y count x ndensity ncount density PANEL group ymin ymax xmin xmax
1 #F8766D 5 5 1 1 1 1.111111 1 1 0 5 0.55 1.45
2 #C49A00 47 47 2 1 1 1.111111 1 2 0 47 1.55 2.45
3 #53B400 41 41 3 1 1 1.111111 1 3 0 41 2.55 3.45
4 #00C094 11 11 4 1 1 1.111111 1 4 0 11 3.55 4.45
5 #00B6EB 33 33 5 1 1 1.111111 1 5 0 33 4.55 5.45
6 #A58AFF 35 35 6 1 1 1.111111 1 6 0 35 5.55 6.45
7 #FB61D7 62 62 7 1 1 1.111111 1 7 0 62 6.55 7.45
52
相關問題
- 1. 如何顛倒ggplot2的默認調色板
- 2. 仿真默認對象#inspect輸出?
- 3. 更改默認的調色板中ggplot
- 4. Wicket:調色板設置默認選擇
- 5. gnuplot的調色板,默認和定義
- 6. ggplot2使用scale_colour_manual訪問默認顏色?
- 7. Overplotting不同顏色的調色板GGPLOT2
- 8. jQuery Mobile默認色板
- 9. 谷歌紙板默認失真值
- 10. iOS 7 - 故事板默認色調顏色
- 11. 如何擴大ggplot2中的調色板
- 12. colorful.core.ColorfulError:顏色「默認」是未知的。使用您的調色板中的色彩(默認:X11 rgb.txt中)
- 13. ggplot2中的默認線型?
- 14. CN1仿真器默認爲屏幕尺寸
- 15. 仿真可移動分隔線:需要禁用默認拖拽
- 16. 仿真IE9中的IE7。什麼是默認?
- 17. Chrome響應式仿真器 - 如何將默認縮放至100%
- 18. django默認頁面時調試=真
- 19. 變色龍模板:默認值'tal:switch'
- 20. FFmpeg過濾器「showpalette」總是返回默認調色板
- 21. 將SDL 1.2的默認調色板與SDL_CreateRGBSurface()匹配?
- 22. 如何更改wicket中的調色板的默認標題?
- 23. 什麼是seaborn 0.2版中圖像的默認調色板?
- 24. Seaborn沒有設置其默認調色板進口
- 25. Howto創建或更改SSRS2016默認調色板?
- 26. 默認藍色
- 27. 如何更改ggplot2的默認背景色?
- 28. 如何在R ggplot2中設置默認的geom_text顏色?
- 29. 如何模仿傳單的默認面板按鈕
- 30. 如何模仿用於WPF控件的默認模板?
看鱗包 – hadley
甜蜜! brewer_pal會非常有用 – SFun28
是的!我在我的辦公桌上保留了一個'display.brewer.all()'的打印輸出。我認爲我喜歡Set1是最好的因素。 –