R语言 使用geom_hex仅绘制高密度数据

kqqjbcuj  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(160)

我使用ggplot和以下代码创建geom_hex绘图:

ggplot(aes(x= inpol, y= polnameref), data =dfbin) +
      geom_hex()

现在,我想生成相同的图表,但只使用高密度数据(计数在前10%的数据)。我希望x轴和y轴的刻度、六边形的大小和颜色保持不变。
我尝试过的方法是使用hexbin创建一个计数列。然后我将 Dataframe 子集化,仅用于高计数和绘图。

h  <- hexbin(dfbin,IDs=TRUE)
cdf   <- h@count[match(h@cID, h@cell)]
dfbin$count <- cdf
          
dfbin <- subset(dfbin, count >= quantile(dfbin$count, 0.9, na.rm = T)) 
ggplot(aes(x= inpol, y= polnameref), data =dfbin) +   
  geom_hex()+ylim(37,80)+xlim(17,65)

这不好。六边形移动了位置。我只想要浅蓝色的。它重置了色标。请帮助。子集之前的 Dataframe 是:

inpol   polnameref
17.42676391 38.25
17.5048993  38.25
17.77483251 38.87
17.39251595 37.63
17.31113196 36.88
17.43306337 40.85
17.35539298 39.93
17.6139507  38.9
17.56161397 40.37
17.73130075 40.5
17.36926083 41.79
17.56821045 40.6
17.69573547 42.78
17.64254034 41.98
17.62787969 40.67
18.10980992 42.85
18.0725116  41.21
18.01466172 40.23
17.95309804 42.56
17.75885604 41.43
18.23903621 42.29
18.07178862 43.25
17.99967031 42.54
20.02494924 42.29
18.51730294 41.73
21.14507957 43.54
20.48827122 45.06
20.3843049  40.77
18.36868867 41.63
19.53477601 41.6
20.80472381 41.75
19.96617762 41.27
19.98848821 41.77
20.02480735 43.51
19.89288626 42.46
20.32677396 42.05
19.66313026 43.08
20.39302774 42.09
20.19763967 41.7
20.42255334 41.43
20.17807805 42.04
20.28806701 42.88
20.40751273 42.66
19.9050178  41.58
20.25198356 41.88
20.20938488 41.67
19.90404658 42.03
19.71477348 42.17
19.68338152 40.98
19.67046427 41.33
19.90519354 41.66
19.71399623 41.72
19.99590555 43.29
20.01490779 41.47
19.70521077 41.96
19.58479633 42.32
19.7858919  41.09
19.48444803 41.93
19.59917189 41.37
19.6844288  42.26
19.45611171 42.2
19.8283006  40.72
19.43650149 42.81
19.45118314 41.68
19.62734881 42.28
19.5755179  42.53
19.30813031 41.44
19.63158924 42.24
19.80666451 42.13
19.79966307 42.74
19.7888629  42.23
19.76426397 42.24
19.27794406 44.97
19.68915206 43.15
19.40696523 41.38
19.31461079 42.82
19.52188744 42.67
19.70922269 41.72
19.74247564 42.05
19.59442694 42.44
19.50884562 42.99
19.47578724 41.87
19.53253826 40.89
19.31143528 42.09
20.06161687 42.74
21.27860503 43.51
21.31562355 44.91
21.63930143 42.82
21.50771959 43.63
21.45356499 44.07
21.58179896 42.72
21.59739348 43.92
21.56413994 44.72
21.94329194 43.01
22.28233901 44.25
21.95846238 43.41
22.37496129 44.35
21.96430959 45.55
22.01903662 44.36
21.78941674 46.48
21.70367342 45.49
21.79222265 44.46
21.78528912 46.66
22.00845113 44.71
21.53516507 45.45
22.17443148 45.84
22.26811528 45.85
22.03964574 46.15
22.04461583 45.24
21.98978594 45.69
22.49674076 45.64
22.10263468 45.19
21.88414276 45.56
22.00298657 45.73
22.15120982 45.63
21.85503077 46.32
22.12098045 45.29
22.14342396 46.42
22.42330741 46.99
22.3987982  46.38
22.44525934 47.62
22.32275898 46.42
22.00197921 46.82
22.38612131 47.01
22.16875464 46.65
22.44285689 46.94
22.47604651 47.34
22.39620617 47.87
22.52473436 48.47
22.21109105 46.91
22.36805704 46.31
22.33986031 47.12
22.25803183 48.03
22.69138134 47.22
22.29976395 47.06
22.33845646 47.05
22.25282078 47.4
22.27895141 47.02
22.55944832 47.4
22.40500726 46.32
22.51406224 47.59
22.37466457 46.83
22.34895404 46.05
22.4610081  46.3
22.34985953 47.3
22.32471725 45.96
22.40504599 46.25
22.34311997 45.39
22.41149132 46.94
22.47033791 45.13
22.21412718 45.44
22.46292823 44.53
22.38438769 44.74
22.1645396  44.46
22.32260146 44.94
22.43829913 46.31
22.34439617 47.86
22.54660265 44.98
22.51751905 46
22.41903293 47.46
22.4024444  45.62
22.64624141 44.83
22.34402646 45.13
22.64857615 46.75
22.628904   46.39
22.03869574 45.26
22.51592044 45.14
22.48721371 46.08
22.59074569 45.41
22.44825156 45.34
22.52951537 48.48
22.55646667 46.85
22.66958166 46.83
22.42873404 47.42
22.54312474 45.71
22.59117514 46.37
22.61624115 47.08
22.68121763 47.2
22.40023094 46.7
22.66173108 45.54
22.37639571 46.56
22.95107637 47.63
23.0379301  47.94
22.72865707 47.62
22.63720396 47.09
22.80593724 48.41
22.72360911 47.59
22.52995956 47
22.45949931 46.79
22.39673331 47.63
22.45988863 46.9
22.70019946 48.56
22.48648674 46.61
22.07587756 47.49
22.60669807 48.34
22.45930076 47.6
22.29895314 47.59
22.27287001 48.23
22.52669014 48.28
22.46398129 48.34
22.12840597 47.15
22.4984977  48.44
22.42495701 48.4
22.55233865 47.04
22.49739963 48.38
22.70725596 47.58
22.36992699 48.49
22.7392806  48.31
22.49460089 48.2
23.03010242 49.61
22.53344863 48.33
22.64603909 47.96
22.81655928 48.7
22.7326182  49.14
22.78408409 49.43
22.52245748 49.4
22.50190243 48.83
22.67152808 51.71
22.73431867 48.66
23.10417711 49.69
22.93608008 50.27
23.41445591 51.35
22.70795555 49.57
23.12595828 49.54
22.89295606 49.9
22.6075502  51.06
21.87559709 51.46
22.6702465  50.35
22.71228948 50.25
23.61207318 49.6
22.65804417 50.71
22.3070406  51.06
22.39958213 49.52
23.5079138  49.97
22.16422586 50.86
22.38312254 49.46
22.04896729 50.63
23.54793995 49.99
20.38162757 49.71
21.38200519 50.54
21.43333337 49.64
21.59500658 50.17
22.03506269 49.49
21.5043559  48.34
21.70015439 49.68
21.80207113 48.69
21.74517136 49.83
21.8609921  49.57
21.67641705 49.55
21.34903313 49.47
21.41308062 48.49
21.63805781 48.97
21.67864117 50.46
21.68675143 48.54
21.62867732 49.51
21.16976344 48.99
21.70387895 47.62
21.477848   49.49
21.9543765  48.85
21.58896211 47.72
21.26003266 49.11
21.35601366 48.58
22.08436729 47.48
22.70243304 45.53
41.2294227  45.61
26.77883723 49.24
36.47645963 48.8
36.8336389  47.23
38.02198933 47.61
38.22273383 45.69
33.14825745 47.6
34.05349207 48.82
34.09730751 47.71
33.11988209 48.16
31.91505055 46.63
31.26936121 44.89
30.23171054 46.84
28.27728546 46.22
26.10252999 45.6
26.56643397 45.63
26.75523342 41.97
27.13260062 46.2
27.22019207 46.03
27.41993138 46.94
27.21529427 46.89
28.11137671 45.27
27.77674591 45.7
27.50241325 46.15
27.31882784 44.74
27.16689129 44.8
27.03591275 44.1
26.53485741 44.66
27.77045211 44.56
26.27928195 44.53
26.04376496 45.8
26.21254574 46.06
26.08243168 45.68
26.12646966 46.19
25.99029321 45.23
25.84735928 46.34
26.19874778 47.84
25.92992329 45.95
26.1647604  47.36
26.22273734 45.69
25.61298895 45.81
25.86863522 44.88
25.54572722 46.39
25.42494037 45.47
25.67121093 46.19
25.22826636 45.27
25.09830832 47.43
25.04649523 46.48
25.15377705 47.37
24.33327451 45.59
24.40491887 44.72
24.74121548 45.12
24.28010796 47.22
24.47110852 44.83
24.59174212 45.93
25.15500895 45.82
25.1112664  46.48
24.84030969 46.43
24.21852486 46.79
24.32798221 47.29
23.70098056 48.09
23.92662019 47.45
24.48271273 48.53
23.99476941 47.91
24.2861086  48.64
24.10086564 49.4
23.84419737 49.03
24.0335352  50.6
24.01178619 50.13
23.67764051 48.53
23.99742496 49.88
23.71466042 49.83
23.76999156 50.26
23.92642523 50.49
23.93898223 49.09
23.46873289 49.58
23.69176247 50.11
24.21102825 48.65
23.71533163 50.24
24.00964442 50.7
23.66545989 50.38
23.83280933 50.75
23.72293199 50.2
23.96290657 51.18
23.96997352 50.89
23.97610611 50.03
24.18524842 51.21
24.44730965 51.28
24.2232263  52.41
24.30968902 52.33
24.18934039 51.98
24.76317041 53.03
24.05379701 53.32
23.99848849 52.23
23.70820653 54.38
23.9988556  55.27
23.92546417 55.85
23.85557109 55.57
24.22741257 54.53
23.34826666 56.29
26.29360721 56.3
43.43981175 75.88
43.43981175 76.15
39.8001966  75.25
39.8001966  76.22
36.34802927 76.09
36.34802927 78.46
33.93190822 78.25
33.93190822 76.6
31.75796316 76.87
31.05566519 78.99
30.98690611 76.77
30.27141269 77.13
30.31065104 76.14
29.99500575 78.37
29.8957148  79.58
29.72999871 77.46
29.49784144 78.35
29.40197634 77.24
29.13645113 79.73
28.91445928 76.69
28.62765673 76.31
28.63034445 76.95
28.61705689 77.64
28.26823504 76.28
28.23742028 76.3
28.39297285 78.13
27.97140031 77.08
27.93802977 75.13
27.74082305 74.32
27.7799947  75.57
27.85760704 76.29
27.80616237 72.74
27.79257959 73.14
27.36234485 71.33
28.017087   73.31
27.20914893 73.78
27.42008989 72.33
27.89608946 73.2
27.60453589 74.37
28.08962092 72.34
27.5358951  71.74
27.9662519  75.09
28.06823197 72.55
28.01134255 71.87
28.12264824 70.65
27.78466708 72.28
27.97271606 69.82
28.12213267 68.82
28.01118324 70.24
27.93769772 68.46
28.10709973 69.37
28.05272866 69.24
27.90094894 69.94
28.36038596 67.39
28.67551687 66.45
28.54934442 68.08
28.35537894 66.08
28.39971046 66.63
28.31906936 66.15
28.41096835 67.26
28.47796523 67.42
28.55833231 67.68
28.71797685 66.2
28.97345694 66.43
28.4854693  64.5
28.37992181 64.28
28.50348589 63.54
27.9512602  64.1
28.29111012 64.35
28.45047576 64.84
28.34868189 64.05
28.34377334 66.04
28.18122859 64.08
28.48492086 64.98
28.20915728 65.17
28.26514517 65.77
28.42044105 65.43
28.18027759 66.08
28.52852303 64.96
28.53784806 66.17
28.29702085 65.51
28.52164566 64.77
28.35108176 64.5
28.52678661 62.02
28.23239935 61.4
28.33353525 62.61
28.3918793  65.79
28.34785098 62.43
28.90676545 65.3
28.86436173 63.42
28.9000225  61.56
28.82531793 61.44
29.13606601 62.29
29.24239893 62.69
30.41295185 62.72
35.29279472 62.92
40.07560578 63.94
39.85047507 62.58
50.88022001 61.68
64.94580963 65.5
61.19567058 64.58
62.02008307 65.03
61.5814782  64.13
60.4794675  63.72
59.44409362 63.79
59.30328613 63.12
58.44514404 63.24
57.9451571  63.23
57.5200673  64.43
56.84759076 62.92
56.27230232 64.49
55.8781137  63.98
55.64307744 62.88
54.67155688 63.24
53.65561172 61.65
53.42913519 63.13
52.54638999 64.15
52.03003247 61.31
51.80103998 62.99
50.95914709 62.6
50.73235272 61.01
51.94559811 64.35
48.87871926 62.41
49.94153858 61.76
49.01587109 62.06
48.36524521 62.33
48.6064699  62.92
47.91984551 62.23
47.73724565 61.86
47.46788407 63.99
47.00676788 60.22
46.78894496 63.7
46.24485433 63.68
45.83904756 62.4
45.95075114 63.9
46.70190761 63.62
43.67902274 65.3
44.81360341 64.09
44.9267436  63.47
44.872148   61.92
44.80256577 65.51
44.30309988 62.88
43.84937295 63.7
44.29863103 64.55
43.46762553 62.95
43.52759219 65.72
42.94900577 66.19
43.02078156 66.81
43.60463837 64.76
43.57744781 61.5
43.5355533  61.4
42.9092125  59.05
42.91910179 62.53
42.9216313  65.57
42.51388867 62.17
42.41387841 66.49
42.13209007 66.71
43.57322997 65.3
43.89428494 63.22
43.26211357 65.08
41.47403191 61.21
40.95912227 64.11
43.1017305  66.42
40.6548483  64.67
42.53598348 64.05
42.7936141  63.7
44.72244582 64.42
44.38661171 61.25
45.03052152 65.03
46.9433944  68.38
48.15864844 68.08
49.22129432 64.63
49.11202257 64.92
50.59884737 65.97
51.89563567 66.55
51.63078624 67.32
qij5mzcb

qij5mzcb1#

设定色阶限制:

scale_fill_gradient(
   limits = c(custom_limit, NA), 
   na.value = NA
)

相关问题