配色

 

 ggplot2 只接受数据框类型

参考博客:

https://blog.csdn.net/chang349276/article/details/77476848

https://blog.csdn.net/songzhilian22/article/details/49388677

http://blog.sciencenet.cn/home.php?mod=space&uid=252888

 

 

 

颜色:

ggplot2涉及到的颜色主要由两种参数函数指定:

 

    • scale_color_manual()    for lines and points
    • scale_fill_manual()         for box plot, bar plot, violin plot, etc 

# Box plot bp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))

# Scatter plot sp + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9")) 

 

# Box plot bp + scale_fill_manual(breaks = c("2", "1", "0.5"), values=c("red", "blue", "green"))

# Scatter plot sp + scale_color_manual(breaks = c("8", "6", "4"), values=c("red", "blue", "green")) 

 

bp + scale_fill_manual(values = alpha(c("blue", "red", "yellow"), .3))     透明度

 

连续型:

  • scale_colour_gradient()和scale_fill_gradient():双色梯度。

顺序由低到高,参数low 和high 用于控制此梯度两端颜色;

  • scale_colour_gradient2()和scale_fill_gradient2():三色梯度。

顺序为低-中-高,参数low和high 用于控制此梯度两端颜色,中点默认值是0,可以用参数midpoint 将其设置为任意值;

  • scale_colour_gradientn()和scale_fill_gradientn():自定义的n 色梯度。

此标度需要赋给参数colours 一个颜色向量。不加其他参数的话,这些颜色将依照数据的范围均匀地分布。如果你需要让这些值不均匀地分布,则可以使用参数values。如果参数rescale 的值是TRUE(默认),则values 应在0 和1 之间取值,如果rescale 取值FALSE,则values 应在数据范围内取值。

 

 

 

> library(reshape2)
> volcano3d <- melt(volcano)

> head(volcano3d)
Var1 Var2 value
1 1 1 100
2 2 1 101
3 3 1 102
4 4 1 103
5 5 1 104
6 6 1 105
> names(volcano3d) <- c("x", "y", "z")

> p <- ggplot(volcano3d,aes(x,y,fill=z)) + geom_tile()
> p
> p + scale_fill_gradient(limits=c(120,170))
> p + scale_fill_gradient(low = 'blue', high = 'red')

 

规定渐变色中间色:midpoint = 150

> p + scale_fill_gradient2(low = 'blue', high = 'red', midpoint = 150)
>
> p + scale_fill_gradient2(low = 'blue', high = 'red')

 

 

 离散型:

使用调色板

scale_colour_ brewer ()

scale_fill_ brewer ()

 

> RColorBrewer::display.brewer.all()

所有调色板:

练习:

> dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
> dsamp
# A tibble: 1,000 x 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.78 Ideal F SI1 61.3 56 3362 5.92 6 3.64
2 0.39 Premium G VS1 62.6 58 855 4.66 4.7 2.93
3 0.27 Ideal F VS2 61.9 54 583 4.14 4.16 2.57
4 0.33 Very Good F IF 60.3 58 1014 4.49 4.46 2.7
5 0.41 Ideal G VS1 60.8 56 899 4.79 4.82 2.92
6 0.37 Premium I VVS1 62.4 58 681 4.55 4.58 2.85
7 1.2 Premium G VS2 61.4 60 7603 6.77 6.81 4.17
8 1.21 Very Good H SI2 62.6 61 5324 6.7 6.75 4.21
9 0.4 Premium G SI1 62.8 60 900 4.7 4.66 2.94
10 1.51 Very Good E VVS2 60.2 61 17689 7.34 7.4 4.44
# ... with 990 more rows
> nrow(diamonds)
[1] 53940
> sample(nrow(diamonds), 1000)
[1] 33494 6581 18729 7852 44449 675 26466 2976 9131 11986 20415 7476 53118 28212
[15] 10324 24236 14808 27571 16818 52725 28419 21971 22196 18842 39559 14984 24731 29714
[29] 48343 27717 10833 17043 6539 39127 47536 46740 50972 52661 20264 49636 41599 42537
[43] 52510 899 12374 15168 46897 17556 49386 12472 43288 6578 50471 41308 20344 45733
[57] 1436 28247 44037 37210 28171 40697 13429 27070 15883 19017 51589 12802 10773 44604
[71] 33595 21818 50243 18674 2512 20763 26761 8110 24490 43103 37200 38792 25333 34606
[85] 3059 25063 3675 39814 30021 44618 1971 51261 15113 18609 30042 1614 6282 51773
[99] 12222 19335 44204 38030 6183 43112 47499 5364 1378 16229 34648 35222 46881 32312
[113] 5294 21390 18771 34957 48842 27094 20138 26647 23853 31873 5382 34627 12993 38536
[127] 47303 6391 35724 25665 36923 47110 16929 5479 31850 4000 16104 3043 2828 48183
[141] 45908 53050 6355 45913 13039 42872 14470 50018 14566 47864 23627 29130 4229 12484
[155] 42925 41396 43130 38687 5554 26596 26781 25634 47378 28740 14137 53906 49723 1451
[169] 39057 5999 5895 43372 6195 5461 14906 1107 10902 8754 27441 12475 17055 2452
[183] 30292 43297 44835 41045 50893 1679 12933 49628 53803 3186 33916 26230 20280 9799
[197] 28897 6093 12244 35779 3593 37625 18282 28630 23750 47324 1641 27013 29599 40253
[211] 23994 39363 41840 16655 16044 32475 12906 29248 21389 27881 16722 32261 31478 17820
[225] 43843 12894 233 1197 21216 16448 4466 25808 11037 50636 42484 49622 26066 22631
[239] 48504 18632 46045 25149 14605 45172 35596 24885 7256 36928 29551 7229 7002 43888
[253] 14401 39496 29793 240 24078 32951 44529 39848 11067 6778 33097 27629 42712 8537
[267] 12547 52551 31127 37168 32067 17927 52185 40509 19230 12707 31576 41637 50173 22904
[281] 7357 28237 28826 42846 39435 46859 43147 30820 1413 16162 1957 26335 12675 32179
[295] 12653 33413 20104 24458 11004 5986 6238 674 20259 13946 16993 6867 2592 30160
[309] 41714 39881 48185 33351 51818 7368 40512 45443 19881 1274 38649 37560 10069 26717
[323] 1785 17522 1485 49250 11002 45256 36119 36080 33168 52401 11163 19339 45690 18026
[337] 26216 44819 50234 7415 4991 49426 6468 14948 41710 7210 31347 49292 1541 18956
[351] 33240 43350 383 32307 8058 9624 29141 39492 23180 14261 34993 2402 27963 12090
[365] 19526 5679 32749 8838 4907 29898 48006 51576 38745 13072 47522 5252 19149 36843
[379] 47973 17598 19570 25046 12366 28208 23329 37660 19722 49731 7215 19256 24631 14030
[393] 328 28530 30791 16439 22110 28619 53606 36537 19911 12294 11054 20617 40208 26637
[407] 4580 36401 9439 26197 5192 21712 14325 44991 20212 400 43666 47658 25187 44460
[421] 17370 41670 6154 30740 11242 14297 49891 12160 33050 12856 37205 14466 27741 25392
[435] 29787 1434 15894 52073 31420 4278 30241 12191 33907 9005 28502 5789 17642 7886
[449] 25988 39942 53746 32249 53373 10412 37908 26537 9456 11154 47666 22022 31941 97
[463] 40330 25795 23902 50735 2368 6856 15271 24847 8080 19601 22793 30724 6327 40523
[477] 8630 28791 5910 42729 5353 52383 33007 49911 44977 19948 45974 12525 1307 17654
[491] 12734 2884 26854 45004 21800 987 44190 22250 44471 53358 7743 11109 48348 35831
[505] 38653 35766 19726 49810 34858 785 26006 10376 28336 27199 37301 40364 6590 19502
[519] 46516 11600 6405 28315 13635 34814 24689 1592 53554 38224 26568 6809 29688 14847
[533] 1479 20267 2938 23461 35023 41581 43141 48593 20931 50343 20804 14011 24925 27831
[547] 19828 4964 14819 17627 3378 23723 17189 14311 46403 2532 15224 17689 46623 11255
[561] 8343 38741 36751 38407 13886 52561 21013 14878 4926 2523 48577 12400 5309 33092
[575] 23265 398 45996 34790 47782 18235 21618 53580 6057 32893 12201 28711 14489 44887
[589] 3802 12459 12228 43052 33175 41456 31157 9183 119 37708 6127 4491 5842 47477
[603] 4467 21901 39361 51680 29421 21370 2257 39703 42771 52429 48104 39588 8780 10856
[617] 19900 16776 27725 20093 28103 35006 34390 14499 47689 28887 50088 51462 28332 42657
[631] 52284 7669 33335 103 31383 30575 26709 3370 53875 31458 7040 25961 430 34707
[645] 13210 21569 8790 8 45534 5416 53828 49929 30048 20082 36395 40626 29648 27732
[659] 37580 23069 42159 14839 37130 12854 35549 28653 30193 40023 5408 34391 1802 7517
[673] 6303 47481 9080 1483 29280 29092 13969 44929 48673 12904 30853 49140 15825 4003
[687] 47441 17684 43800 44719 36075 8119 52988 48559 1958 28632 47993 50549 33398 37171
[701] 30621 3460 31119 21172 10208 6555 16545 48700 10229 551 12655 51995 24900 3261
[715] 29347 50211 30610 18975 25995 1720 3103 7866 39063 18635 21021 38672 8254 36774
[729] 29491 1950 22465 5280 15302 4551 51611 43381 26589 45512 47997 22366 10040 38599
[743] 20236 41016 1113 36552 9211 51559 46982 5922 13851 11847 2717 27919 36829 46437
[757] 26914 35364 53852 33997 813 19290 25622 36800 19473 44072 18875 41492 22481 18994
[771] 53055 3726 15428 21146 48093 29982 6567 3938 939 43511 14417 11813 24257 3613
[785] 21753 30093 30945 13787 34050 839 3026 23115 41077 34279 20451 21296 24577 9750
[799] 11046 8904 28939 14286 8826 30010 12668 39845 53883 43982 25813 5618 28787 26556
[813] 16593 18125 31789 332 16225 35750 2919 14697 14154 24890 6678 39843 28611 27778
[827] 42260 30886 42043 4031 13580 45911 4677 4972 19573 12183 51593 28405 45527 37909
[841] 8965 42323 23900 20889 23717 31006 36046 46244 45473 30816 9119 30416 25901 52580
[855] 25002 5802 52082 9671 25222 50863 24455 16519 46571 40759 22812 35807 22650 33889
[869] 17342 17551 3785 17284 35947 49050 13074 42969 38074 5176 6770 11858 16328 2786
[883] 37341 4669 40806 17306 3937 31712 31053 29743 20029 28243 12357 31623 15665 914
[897] 32811 38351 19969 34404 2329 966 11309 18675 12265 40184 8919 35712 51122 21026
[911] 53927 13543 11023 35585 22742 4226 12122 38655 13996 24906 26836 31491 16755 50196
[925] 45377 12491 17293 33570 14288 22149 43279 41254 38099 45600 11107 50738 35964 3016
[939] 35474 11965 48366 16947 35099 29564 28996 39288 1460 35040 50100 1058 19262 39404
[953] 8309 45282 34674 39169 21270 46527 35495 35263 49583 9746 7193 31651 13381 22452
[967] 30236 51622 7970 17970 47935 35354 43988 24558 807 6413 36984 52454 25887 30456
[981] 26755 10228 47373 29758 46100 9516 50519 23025 20660 31978 12326 43952 8012 50876
[995] 38320 25368 32505 51018 5431 53792
> d <- ggplot(dsamp, aes(cut, price, fill = cut)) + geom_boxplot()

 


> d + scale_fill_brewer()
>

 

> d + scale_fill_brewer(palette = 2)

 > d + scale_fill_brewer(palette = "Spectral")

> d + scale_fill_brewer(palette ="OrRd")
> d + scale_fill_brewer(palette ="PuBu")
> d + scale_fill_brewer(palette ="Set3")
> d + scale_fill_brewer(palette ="BrBG")

 

转载于:https://www.cnblogs.com/LXiaoQ/p/9752851.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值