更改连续比例尺 ggplot 的标签
Change labeling of continuous scale bar ggplot
我什至不确定这是否可以用连续的比例尺来做,但我试了一下 ;)
我有这个数据:
return_cluster
year variable value
1 1988 V1 1.721150
2 1989 V1 5.288607
3 1990 V1 63.355563
4 1991 V1 47.516021
5 1992 V1 48.766120
6 1993 V1 20.463455
7 1994 V1 1.116961
8 1995 V1 1.804225
9 1996 V1 5.159672
10 1997 V1 2.342435
11 1998 V1 1.831826
12 1999 V1 2.916135
13 2000 V1 2.096115
14 2001 V1 2.205473
15 2002 V1 97.766868
16 2003 V1 1.395028
17 2004 V1 14.778782
18 2005 V1 21.626368
19 2006 V1 335.569275
20 2007 V1 2.577229
21 2008 V1 46.794667
22 2009 V1 18.456694
23 2010 V1 28.347202
24 2011 V1 1.754569
25 2012 V1 1.967250
26 2013 V1 1.355391
27 2014 V1 2.310848
28 2015 V1 1.236857
29 2016 V1 72.404651
30 2017 V1 327.830361
31 1988 V2 1.602976
32 1989 V2 144.354881
33 1990 V2 98.927558
34 1991 V2 47.287984
35 1992 V2 47.254139
36 1993 V2 50.085161
37 1994 V2 1.519317
38 1995 V2 1.290500
39 1996 V2 27.165630
40 1997 V2 3.708591
41 1998 V2 2.900900
42 1999 V2 32.971143
43 2000 V2 1.783584
44 2001 V2 1.556819
45 2002 V2 2.397101
46 2003 V2 2.100313
47 2004 V2 1.878331
48 2005 V2 59.814862
49 2006 V2 561.607269
50 2007 V2 18.060577
51 2008 V2 24.921608
52 2009 V2 25.684559
53 2010 V2 30.211938
54 2011 V2 14.210188
55 2012 V2 19.619167
56 2013 V2 1.614514
57 2014 V2 9.915848
58 2015 V2 46.625465
59 2016 V2 2.088176
60 2017 V2 27.044079
61 1988 V3 3.491779
62 1989 V3 2.808945
63 1990 V3 91.647147
64 1991 V3 3.398350
65 1992 V3 50.143573
66 1993 V3 183.457528
67 1994 V3 1.281821
68 1995 V3 1.875897
69 1996 V3 371.005217
70 1997 V3 2.425475
71 1998 V3 18.015665
72 1999 V3 1.419050
73 2000 V3 47.724070
74 2001 V3 1.143962
75 2002 V3 1.951659
76 2003 V3 2.676810
77 2004 V3 2.597871
78 2005 V3 69.679082
79 2006 V3 7.110426
80 2007 V3 47.511509
81 2008 V3 1.880928
82 2009 V3 1.988084
83 2010 V3 83.713620
84 2011 V3 31.541859
85 2012 V3 1.897654
86 2013 V3 2.341847
87 2014 V3 3.733591
88 2015 V3 23.842150
89 2016 V3 13.638751
90 2017 V3 1.712088
91 1988 V4 1.486628
92 1989 V4 1.407240
93 1990 V4 26.211749
94 1991 V4 67.534555
95 1992 V4 92.307045
96 1993 V4 3.528735
97 1994 V4 17.328366
98 1995 V4 4.674465
99 1996 V4 88.268235
100 1997 V4 132.922890
101 1998 V4 4.439413
102 1999 V4 1.401003
103 2000 V4 2.812608
104 2001 V4 1.526576
105 2002 V4 4.921232
106 2003 V4 51.542854
107 2004 V4 3.860508
108 2005 V4 2.243960
109 2006 V4 2.507551
110 2007 V4 18.467204
111 2008 V4 4.301163
112 2009 V4 1.188909
113 2010 V4 186.013334
114 2011 V4 260.879110
115 2012 V4 1.833610
116 2013 V4 69.432973
117 2014 V4 9.092139
118 2015 V4 1.710509
119 2016 V4 1.442288
120 2017 V4 21.648317
121 1988 V5 1.124042
122 1989 V5 5.834290
123 1990 V5 18.423492
124 1991 V5 71.584537
125 1992 V5 48.946872
126 1993 V5 3.994614
127 1994 V5 1.375991
128 1995 V5 1.389525
129 1996 V5 4.434723
130 1997 V5 3.192515
131 1998 V5 57.507683
132 1999 V5 1.107362
133 2000 V5 1.440586
134 2001 V5 1.216205
135 2002 V5 1.601559
136 2003 V5 4.691851
137 2004 V5 2.350900
138 2005 V5 4.148232
139 2006 V5 2.560688
140 2007 V5 161.408504
141 2008 V5 1.526090
142 2009 V5 2.304330
143 2010 V5 3.424447
144 2011 V5 251.419437
145 2012 V5 1.477274
146 2013 V5 1.403403
147 2014 V5 2.473882
148 2015 V5 1.708639
149 2016 V5 2.773274
150 2017 V5 6.344827
151 1988 V6 1.518616
152 1989 V6 58.778503
153 1990 V6 2.912868
154 1991 V6 2.684705
155 1992 V6 24.784557
156 1993 V6 47.099474
157 1994 V6 1.901775
158 1995 V6 16.253749
159 1996 V6 11.504747
160 1997 V6 2.819182
161 1998 V6 19.980632
162 1999 V6 1.390191
163 2000 V6 4.207280
164 2001 V6 1.332001
165 2002 V6 3.676687
166 2003 V6 208.344203
167 2004 V6 4.790638
168 2005 V6 68.214507
169 2006 V6 244.268239
170 2007 V6 8.262237
171 2008 V6 1.743053
172 2009 V6 19.925573
173 2010 V6 2.313235
174 2011 V6 265.988933
175 2012 V6 1.549337
176 2013 V6 1.484683
177 2014 V6 135.733452
178 2015 V6 2.846374
179 2016 V6 1.094535
180 2017 V6 148.108424
181 1988 V7 1.686723
182 1989 V7 7.227748
183 1990 V7 35.340633
184 1991 V7 10.835810
185 1992 V7 2.138298
186 1993 V7 1.575984
187 1994 V7 10.129774
188 1995 V7 50.201159
189 1996 V7 13.209625
190 1997 V7 2.184044
191 1998 V7 16.584965
192 1999 V7 2.480640
193 2000 V7 5.299119
194 2001 V7 1.319700
195 2002 V7 2.265038
196 2003 V7 355.111101
197 2004 V7 21.919797
198 2005 V7 104.912027
199 2006 V7 254.276631
200 2007 V7 49.192583
201 2008 V7 24.358314
202 2009 V7 11.218015
203 2010 V7 14.356157
204 2011 V7 48.386913
205 2012 V7 7.390883
206 2013 V7 4.089435
207 2014 V7 1.693219
208 2015 V7 297.646638
209 2016 V7 1.670901
210 2017 V7 29.193866
211 1988 V8 4.291061
212 1989 V8 10.147915
213 1990 V8 45.486480
214 1991 V8 68.942930
215 1992 V8 16.576701
216 1993 V8 11.493921
217 1994 V8 8.940650
218 1995 V8 47.943372
219 1996 V8 7.088878
220 1997 V8 11.432932
221 1998 V8 76.057542
222 1999 V8 1.912122
223 2000 V8 2.881820
224 2001 V8 17.467523
225 2002 V8 1.830679
226 2003 V8 353.781607
227 2004 V8 3.107457
228 2005 V8 35.021645
229 2006 V8 64.324620
230 2007 V8 59.491304
231 2008 V8 24.755776
232 2009 V8 2.546088
233 2010 V8 1.617441
234 2011 V8 3.438010
235 2012 V8 12.320183
236 2013 V8 13.426661
237 2014 V8 1.687504
238 2015 V8 109.999377
239 2016 V8 5.236363
240 2017 V8 14.251569
241 1988 V9 11.798883
242 1989 V9 38.970656
243 1990 V9 95.412293
244 1991 V9 162.053168
245 1992 V9 10.486107
246 1993 V9 62.022276
247 1994 V9 1.305770
248 1995 V9 46.413613
249 1996 V9 10.403840
250 1997 V9 68.976595
251 1998 V9 10.699454
252 1999 V9 2.762492
253 2000 V9 11.052923
254 2001 V9 2.285492
255 2002 V9 2.210362
256 2003 V9 363.602498
257 2004 V9 49.067139
258 2005 V9 21.810117
259 2006 V9 10.043886
260 2007 V9 23.549936
261 2008 V9 24.546635
262 2009 V9 157.500968
263 2010 V9 2.152319
264 2011 V9 2.684216
265 2012 V9 9.913968
266 2013 V9 10.366976
267 2014 V9 3.802875
268 2015 V9 62.889429
269 2016 V9 36.761245
270 2017 V9 27.634371
271 1988 V10 24.666011
272 1989 V10 187.050844
273 1990 V10 61.212670
274 1991 V10 23.364418
275 1992 V10 18.781973
276 1993 V10 1.101492
277 1994 V10 4.368106
278 1995 V10 78.504693
279 1996 V10 2.042534
280 1997 V10 208.185547
281 1998 V10 9.015323
282 1999 V10 1.846591
283 2000 V10 1.535912
284 2001 V10 24.826558
285 2002 V10 10.650948
286 2003 V10 207.507455
287 2004 V10 20.041347
288 2005 V10 101.049600
289 2006 V10 50.859651
290 2007 V10 41.764536
291 2008 V10 18.482884
292 2009 V10 344.989241
293 2010 V10 36.112390
294 2011 V10 78.213088
295 2012 V10 1.848186
296 2013 V10 2.696125
297 2014 V10 56.530760
298 2015 V10 48.419746
299 2016 V10 87.035051
300 2017 V10 26.644928
301 1988 V11 70.310367
302 1989 V11 174.213909
303 1990 V11 15.481841
304 1991 V11 15.031165
305 1992 V11 23.790369
306 1993 V11 1.751277
307 1994 V11 3.166003
308 1995 V11 57.540285
309 1996 V11 2.287525
310 1997 V11 20.564880
311 1998 V11 17.683897
312 1999 V11 1.706362
313 2000 V11 1.411072
314 2001 V11 9.545351
315 2002 V11 5.165300
316 2003 V11 12.511814
317 2004 V11 54.369030
318 2005 V11 324.799491
319 2006 V11 18.882354
320 2007 V11 111.124844
321 2008 V11 1.890256
322 2009 V11 18.330233
323 2010 V11 1.681225
324 2011 V11 268.381319
325 2012 V11 1.590532
326 2013 V11 1.217613
327 2014 V11 1.221466
328 2015 V11 172.429858
329 2016 V11 18.171812
330 2017 V11 13.392772
331 1988 V12 13.568733
332 1989 V12 283.648545
333 1990 V12 28.669739
334 1991 V12 54.023489
335 1992 V12 12.933720
336 1993 V12 5.902544
337 1994 V12 12.104002
338 1995 V12 53.059435
339 1996 V12 1.565862
340 1997 V12 23.069261
341 1998 V12 34.233273
342 1999 V12 6.442077
343 2000 V12 7.617126
344 2001 V12 66.787312
345 2002 V12 1.324712
346 2003 V12 27.446611
347 2004 V12 82.908726
348 2005 V12 364.147874
349 2006 V12 39.927251
350 2007 V12 54.989735
351 2008 V12 2.448610
352 2009 V12 5.168975
353 2010 V12 1.674610
354 2011 V12 11.135477
355 2012 V12 3.013439
356 2013 V12 5.467428
357 2014 V12 2.414924
358 2015 V12 101.976833
359 2016 V12 209.803373
360 2017 V12 18.037623
我用这段代码绘制它:
ggplot(return_cluster, mapping = aes(x = variable, y = year, fill = value)) +
geom_raster() +
scale_y_continuous(trans = "reverse") +
scale_fill_gradientn(colors = c("blue4", "dodgerblue1", "green3", "yellow", "red"), limits = c(0,500), name = "value", breaks = c(10,20,50,100,500))
您可能已经注意到我更改了比例尺的标签间隔。但我试图实现,只有标签的名称发生变化,但每个标签之间的距离保持不变,就好像我在 100, 200, 300, 400 and 500
处使用 breaks
默认标签一样。
所以黑线从上到下应该代表500, 100, 50, 20, 10
@evian_bottle 很高兴听到有帮助。解决策略大致可以分为
- self-transformation,例如连续范围的装箱或数据的其他映射。从概念上讲,您可以 transform/mutate 您的值范围以满足您的需求,即建议的分箱只是众多转换之一。您可以映射到对数刻度或归一化等。
- 使用 ggplot 的内部转换支持。一组转换有 built-in-support(check-out 第 10.1.9 节 https://ggplot2-book.org/scale-position.html)。此选项的好处是您不必调整图例。
(我)self-transformation
在“合并方法”下方。您可以使用此技术将您的 value-ranges“转换”为对您的 research/project 有意义的内容。正如建议的那样,我为值的范围定义了一个中断向量,即 my_breaks.
# define your break point "transformation" (= new / transformed variable)
my_breaks <- c(0,5,10, 20, 50, 100, 200,500,Inf)
return_cluster %>%
mutate( bin = cut(value, breaks = my_breaks) ) %>%
# point fill aesthetic to bin variable
ggplot(mapping = aes(x = variable, y = year, fill = bin)) +
geom_raster() +
scale_y_continuous(trans = "reverse") +
# define your colour coding
# note: I took a shortcut here as dependent on the number of breaks
# you can define your colour steps. Still, I suggest to use a built-in palette to
# ease the pain of defining endless colours.
# Note: direction parameter reverses the Spectral colours to match blue := low
scale_fill_brewer(palette="Spectral", direction=-1)
这导致以下结果(可以说是视觉美学的“优化”颜色):
您可以根据自己的喜好调整图例标题和标签,例如定义标题和标签文本。 (查看文档或此处的 SO)
(II) ggplot的built-in转换支持
如果你想使用 ggplot 的 built-in transformation 支持,你可以使用 trans= 参数。
return_cluster %>%
ggplot(mapping = aes(x = variable, y = year, fill = value)) +
geom_raster() +
scale_y_continuous(trans = "reverse") +
# set trans parameter to "logarithmic" (to amplify lower values
# and reduce spread on higher values) - check doc for other options
scale_fill_gradient( name = "value", trans = "log"
,breaks = my_breaks, labels = my_breaks
# took shortcut re colour coding - adapt to your liking/visual aesthetics
,low = "blue4", high = "red")
这产生:
图例来自 out-of-the-box ggplot。严格来说,图例的间距不均匀(正如您最初寻找的那样)。但同样,这是一个修补完美主义与开箱即用的问题。
祝你工作顺利!
我什至不确定这是否可以用连续的比例尺来做,但我试了一下 ;)
我有这个数据:
return_cluster
year variable value
1 1988 V1 1.721150
2 1989 V1 5.288607
3 1990 V1 63.355563
4 1991 V1 47.516021
5 1992 V1 48.766120
6 1993 V1 20.463455
7 1994 V1 1.116961
8 1995 V1 1.804225
9 1996 V1 5.159672
10 1997 V1 2.342435
11 1998 V1 1.831826
12 1999 V1 2.916135
13 2000 V1 2.096115
14 2001 V1 2.205473
15 2002 V1 97.766868
16 2003 V1 1.395028
17 2004 V1 14.778782
18 2005 V1 21.626368
19 2006 V1 335.569275
20 2007 V1 2.577229
21 2008 V1 46.794667
22 2009 V1 18.456694
23 2010 V1 28.347202
24 2011 V1 1.754569
25 2012 V1 1.967250
26 2013 V1 1.355391
27 2014 V1 2.310848
28 2015 V1 1.236857
29 2016 V1 72.404651
30 2017 V1 327.830361
31 1988 V2 1.602976
32 1989 V2 144.354881
33 1990 V2 98.927558
34 1991 V2 47.287984
35 1992 V2 47.254139
36 1993 V2 50.085161
37 1994 V2 1.519317
38 1995 V2 1.290500
39 1996 V2 27.165630
40 1997 V2 3.708591
41 1998 V2 2.900900
42 1999 V2 32.971143
43 2000 V2 1.783584
44 2001 V2 1.556819
45 2002 V2 2.397101
46 2003 V2 2.100313
47 2004 V2 1.878331
48 2005 V2 59.814862
49 2006 V2 561.607269
50 2007 V2 18.060577
51 2008 V2 24.921608
52 2009 V2 25.684559
53 2010 V2 30.211938
54 2011 V2 14.210188
55 2012 V2 19.619167
56 2013 V2 1.614514
57 2014 V2 9.915848
58 2015 V2 46.625465
59 2016 V2 2.088176
60 2017 V2 27.044079
61 1988 V3 3.491779
62 1989 V3 2.808945
63 1990 V3 91.647147
64 1991 V3 3.398350
65 1992 V3 50.143573
66 1993 V3 183.457528
67 1994 V3 1.281821
68 1995 V3 1.875897
69 1996 V3 371.005217
70 1997 V3 2.425475
71 1998 V3 18.015665
72 1999 V3 1.419050
73 2000 V3 47.724070
74 2001 V3 1.143962
75 2002 V3 1.951659
76 2003 V3 2.676810
77 2004 V3 2.597871
78 2005 V3 69.679082
79 2006 V3 7.110426
80 2007 V3 47.511509
81 2008 V3 1.880928
82 2009 V3 1.988084
83 2010 V3 83.713620
84 2011 V3 31.541859
85 2012 V3 1.897654
86 2013 V3 2.341847
87 2014 V3 3.733591
88 2015 V3 23.842150
89 2016 V3 13.638751
90 2017 V3 1.712088
91 1988 V4 1.486628
92 1989 V4 1.407240
93 1990 V4 26.211749
94 1991 V4 67.534555
95 1992 V4 92.307045
96 1993 V4 3.528735
97 1994 V4 17.328366
98 1995 V4 4.674465
99 1996 V4 88.268235
100 1997 V4 132.922890
101 1998 V4 4.439413
102 1999 V4 1.401003
103 2000 V4 2.812608
104 2001 V4 1.526576
105 2002 V4 4.921232
106 2003 V4 51.542854
107 2004 V4 3.860508
108 2005 V4 2.243960
109 2006 V4 2.507551
110 2007 V4 18.467204
111 2008 V4 4.301163
112 2009 V4 1.188909
113 2010 V4 186.013334
114 2011 V4 260.879110
115 2012 V4 1.833610
116 2013 V4 69.432973
117 2014 V4 9.092139
118 2015 V4 1.710509
119 2016 V4 1.442288
120 2017 V4 21.648317
121 1988 V5 1.124042
122 1989 V5 5.834290
123 1990 V5 18.423492
124 1991 V5 71.584537
125 1992 V5 48.946872
126 1993 V5 3.994614
127 1994 V5 1.375991
128 1995 V5 1.389525
129 1996 V5 4.434723
130 1997 V5 3.192515
131 1998 V5 57.507683
132 1999 V5 1.107362
133 2000 V5 1.440586
134 2001 V5 1.216205
135 2002 V5 1.601559
136 2003 V5 4.691851
137 2004 V5 2.350900
138 2005 V5 4.148232
139 2006 V5 2.560688
140 2007 V5 161.408504
141 2008 V5 1.526090
142 2009 V5 2.304330
143 2010 V5 3.424447
144 2011 V5 251.419437
145 2012 V5 1.477274
146 2013 V5 1.403403
147 2014 V5 2.473882
148 2015 V5 1.708639
149 2016 V5 2.773274
150 2017 V5 6.344827
151 1988 V6 1.518616
152 1989 V6 58.778503
153 1990 V6 2.912868
154 1991 V6 2.684705
155 1992 V6 24.784557
156 1993 V6 47.099474
157 1994 V6 1.901775
158 1995 V6 16.253749
159 1996 V6 11.504747
160 1997 V6 2.819182
161 1998 V6 19.980632
162 1999 V6 1.390191
163 2000 V6 4.207280
164 2001 V6 1.332001
165 2002 V6 3.676687
166 2003 V6 208.344203
167 2004 V6 4.790638
168 2005 V6 68.214507
169 2006 V6 244.268239
170 2007 V6 8.262237
171 2008 V6 1.743053
172 2009 V6 19.925573
173 2010 V6 2.313235
174 2011 V6 265.988933
175 2012 V6 1.549337
176 2013 V6 1.484683
177 2014 V6 135.733452
178 2015 V6 2.846374
179 2016 V6 1.094535
180 2017 V6 148.108424
181 1988 V7 1.686723
182 1989 V7 7.227748
183 1990 V7 35.340633
184 1991 V7 10.835810
185 1992 V7 2.138298
186 1993 V7 1.575984
187 1994 V7 10.129774
188 1995 V7 50.201159
189 1996 V7 13.209625
190 1997 V7 2.184044
191 1998 V7 16.584965
192 1999 V7 2.480640
193 2000 V7 5.299119
194 2001 V7 1.319700
195 2002 V7 2.265038
196 2003 V7 355.111101
197 2004 V7 21.919797
198 2005 V7 104.912027
199 2006 V7 254.276631
200 2007 V7 49.192583
201 2008 V7 24.358314
202 2009 V7 11.218015
203 2010 V7 14.356157
204 2011 V7 48.386913
205 2012 V7 7.390883
206 2013 V7 4.089435
207 2014 V7 1.693219
208 2015 V7 297.646638
209 2016 V7 1.670901
210 2017 V7 29.193866
211 1988 V8 4.291061
212 1989 V8 10.147915
213 1990 V8 45.486480
214 1991 V8 68.942930
215 1992 V8 16.576701
216 1993 V8 11.493921
217 1994 V8 8.940650
218 1995 V8 47.943372
219 1996 V8 7.088878
220 1997 V8 11.432932
221 1998 V8 76.057542
222 1999 V8 1.912122
223 2000 V8 2.881820
224 2001 V8 17.467523
225 2002 V8 1.830679
226 2003 V8 353.781607
227 2004 V8 3.107457
228 2005 V8 35.021645
229 2006 V8 64.324620
230 2007 V8 59.491304
231 2008 V8 24.755776
232 2009 V8 2.546088
233 2010 V8 1.617441
234 2011 V8 3.438010
235 2012 V8 12.320183
236 2013 V8 13.426661
237 2014 V8 1.687504
238 2015 V8 109.999377
239 2016 V8 5.236363
240 2017 V8 14.251569
241 1988 V9 11.798883
242 1989 V9 38.970656
243 1990 V9 95.412293
244 1991 V9 162.053168
245 1992 V9 10.486107
246 1993 V9 62.022276
247 1994 V9 1.305770
248 1995 V9 46.413613
249 1996 V9 10.403840
250 1997 V9 68.976595
251 1998 V9 10.699454
252 1999 V9 2.762492
253 2000 V9 11.052923
254 2001 V9 2.285492
255 2002 V9 2.210362
256 2003 V9 363.602498
257 2004 V9 49.067139
258 2005 V9 21.810117
259 2006 V9 10.043886
260 2007 V9 23.549936
261 2008 V9 24.546635
262 2009 V9 157.500968
263 2010 V9 2.152319
264 2011 V9 2.684216
265 2012 V9 9.913968
266 2013 V9 10.366976
267 2014 V9 3.802875
268 2015 V9 62.889429
269 2016 V9 36.761245
270 2017 V9 27.634371
271 1988 V10 24.666011
272 1989 V10 187.050844
273 1990 V10 61.212670
274 1991 V10 23.364418
275 1992 V10 18.781973
276 1993 V10 1.101492
277 1994 V10 4.368106
278 1995 V10 78.504693
279 1996 V10 2.042534
280 1997 V10 208.185547
281 1998 V10 9.015323
282 1999 V10 1.846591
283 2000 V10 1.535912
284 2001 V10 24.826558
285 2002 V10 10.650948
286 2003 V10 207.507455
287 2004 V10 20.041347
288 2005 V10 101.049600
289 2006 V10 50.859651
290 2007 V10 41.764536
291 2008 V10 18.482884
292 2009 V10 344.989241
293 2010 V10 36.112390
294 2011 V10 78.213088
295 2012 V10 1.848186
296 2013 V10 2.696125
297 2014 V10 56.530760
298 2015 V10 48.419746
299 2016 V10 87.035051
300 2017 V10 26.644928
301 1988 V11 70.310367
302 1989 V11 174.213909
303 1990 V11 15.481841
304 1991 V11 15.031165
305 1992 V11 23.790369
306 1993 V11 1.751277
307 1994 V11 3.166003
308 1995 V11 57.540285
309 1996 V11 2.287525
310 1997 V11 20.564880
311 1998 V11 17.683897
312 1999 V11 1.706362
313 2000 V11 1.411072
314 2001 V11 9.545351
315 2002 V11 5.165300
316 2003 V11 12.511814
317 2004 V11 54.369030
318 2005 V11 324.799491
319 2006 V11 18.882354
320 2007 V11 111.124844
321 2008 V11 1.890256
322 2009 V11 18.330233
323 2010 V11 1.681225
324 2011 V11 268.381319
325 2012 V11 1.590532
326 2013 V11 1.217613
327 2014 V11 1.221466
328 2015 V11 172.429858
329 2016 V11 18.171812
330 2017 V11 13.392772
331 1988 V12 13.568733
332 1989 V12 283.648545
333 1990 V12 28.669739
334 1991 V12 54.023489
335 1992 V12 12.933720
336 1993 V12 5.902544
337 1994 V12 12.104002
338 1995 V12 53.059435
339 1996 V12 1.565862
340 1997 V12 23.069261
341 1998 V12 34.233273
342 1999 V12 6.442077
343 2000 V12 7.617126
344 2001 V12 66.787312
345 2002 V12 1.324712
346 2003 V12 27.446611
347 2004 V12 82.908726
348 2005 V12 364.147874
349 2006 V12 39.927251
350 2007 V12 54.989735
351 2008 V12 2.448610
352 2009 V12 5.168975
353 2010 V12 1.674610
354 2011 V12 11.135477
355 2012 V12 3.013439
356 2013 V12 5.467428
357 2014 V12 2.414924
358 2015 V12 101.976833
359 2016 V12 209.803373
360 2017 V12 18.037623
我用这段代码绘制它:
ggplot(return_cluster, mapping = aes(x = variable, y = year, fill = value)) +
geom_raster() +
scale_y_continuous(trans = "reverse") +
scale_fill_gradientn(colors = c("blue4", "dodgerblue1", "green3", "yellow", "red"), limits = c(0,500), name = "value", breaks = c(10,20,50,100,500))
您可能已经注意到我更改了比例尺的标签间隔。但我试图实现,只有标签的名称发生变化,但每个标签之间的距离保持不变,就好像我在 100, 200, 300, 400 and 500
处使用 breaks
默认标签一样。
所以黑线从上到下应该代表500, 100, 50, 20, 10
@evian_bottle 很高兴听到有帮助。解决策略大致可以分为
- self-transformation,例如连续范围的装箱或数据的其他映射。从概念上讲,您可以 transform/mutate 您的值范围以满足您的需求,即建议的分箱只是众多转换之一。您可以映射到对数刻度或归一化等。
- 使用 ggplot 的内部转换支持。一组转换有 built-in-support(check-out 第 10.1.9 节 https://ggplot2-book.org/scale-position.html)。此选项的好处是您不必调整图例。
(我)self-transformation
在“合并方法”下方。您可以使用此技术将您的 value-ranges“转换”为对您的 research/project 有意义的内容。正如建议的那样,我为值的范围定义了一个中断向量,即 my_breaks.
# define your break point "transformation" (= new / transformed variable)
my_breaks <- c(0,5,10, 20, 50, 100, 200,500,Inf)
return_cluster %>%
mutate( bin = cut(value, breaks = my_breaks) ) %>%
# point fill aesthetic to bin variable
ggplot(mapping = aes(x = variable, y = year, fill = bin)) +
geom_raster() +
scale_y_continuous(trans = "reverse") +
# define your colour coding
# note: I took a shortcut here as dependent on the number of breaks
# you can define your colour steps. Still, I suggest to use a built-in palette to
# ease the pain of defining endless colours.
# Note: direction parameter reverses the Spectral colours to match blue := low
scale_fill_brewer(palette="Spectral", direction=-1)
这导致以下结果(可以说是视觉美学的“优化”颜色):
您可以根据自己的喜好调整图例标题和标签,例如定义标题和标签文本。 (查看文档或此处的 SO)
(II) ggplot的built-in转换支持
如果你想使用 ggplot 的 built-in transformation 支持,你可以使用 trans= 参数。
return_cluster %>%
ggplot(mapping = aes(x = variable, y = year, fill = value)) +
geom_raster() +
scale_y_continuous(trans = "reverse") +
# set trans parameter to "logarithmic" (to amplify lower values
# and reduce spread on higher values) - check doc for other options
scale_fill_gradient( name = "value", trans = "log"
,breaks = my_breaks, labels = my_breaks
# took shortcut re colour coding - adapt to your liking/visual aesthetics
,low = "blue4", high = "red")
这产生:
图例来自 out-of-the-box ggplot。严格来说,图例的间距不均匀(正如您最初寻找的那样)。但同样,这是一个修补完美主义与开箱即用的问题。
祝你工作顺利!