如何让比例尺的刻度不再等距?

编程语言 2026-07-10

图片描述

[不等距的刻度条]

我看到一个图像的刻度是这样的。数据既不是对数变换也不是平方根变换,它们只是对刻度进行了“拉伸”。这到底叫什么名字,怎么在R 中实现?有没有专门的术语?我现在用下面的line (lims) 来绘制,但我不想使用它,因为它会在只有较小数值时移除一些真实数据。

lims <- quantile(plot_df$Backscatter, probs = c(0.0, 0.99), na.rm = TRUE)
small_df <- plot_df %>%
   filter(Profile %in% 80:90, depth_m >= 150 & depth_m <= 200) %>%
   select(depth_m, Backscatter) 
dput(small_df)
structure(list(depth_m = c(150, 151, 152, 153, 154, 155, 156, 
157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 
170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 
183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 
196, 197, 198, 199, 200, 150, 151, 152, 153, 154, 155, 156, 157, 
158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 
171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 
184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 
197, 198, 199, 200, 150, 151, 152, 153, 154, 155, 156, 157, 158, 
159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 
172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 
198, 199, 200, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 
173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 
186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 
199, 200, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 
174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 
187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 
200, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 
162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 
175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 
188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 
150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 
163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 
189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 150, 
151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 
164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 
177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 
190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 150, 151, 
152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 
165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 
178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 
191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 150, 151, 152, 
153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 
166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 
179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 
192, 193, 194, 195, 196, 197, 198, 199, 200, 150, 151, 152, 153, 
154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 
167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 
180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 
193, 194, 195, 196, 197, 198, 199, 200), Backscatter = c(0.000238605938609155, 
0.00022687678733237, 0.00024961429134182, 0.000249836956879316, 
0.00026220171120713, 0.000337835483556937, 0.000260126948670306, 
0.000236206243447326, 0.000278774632478963, 0.000222873499497659, 
0.000220210346768622, 0.000219584, 0.000232536064096367, 0.000213365266489205, 
0.000245979090452825, 0.000267173508233184, 0.000226446, 0.00024688359748811, 
0.000296749968332301, 0.000236345144548273, 0.000237821819019873, 
0.000208332818379258, 0.000238001331320361, 0.000233906779692212, 
0.000206004660028292, 0.000208321040539789, 0.000277181822682031, 
0.00020284185649026, 0.00029445160349897, 0.000227561049404753, 
0.000268666020904064, 0.000279458351070314, 0.000236662461169422, 
0.000250014089288469, 0.000308946106109883, 0.000355260978539483, 
0.00022344248029291, 0.000267676079055949, 0.00020586, 0.0002218551466701, 
0.000237470867973726, 0.000238202804648554, 0.000217137477441388, 
0.000261233044389472, 0.000262315917239646, 0.000235497984920577, 
0.000249583505111668, 0.00021123483928352, 0.000241778672495118, 
0.00019376310670745, 0.000290846809289035, 0.00021536728845725, 
0.000256887602457436, 0.000249422744286322, 0.000205484828610651, 
0.000232708021684216, 0.000228007708540255, 0.000231893570033148, 
0.000246865065112318, 0.000379174299963496, 0.000211118814537664, 
0.000209919515729634, 0.000223551602192527, 0.000230565006834923, 
0.000421659431625006, 0.000659640580400077, 0.000221922380489151, 
0.00064174815703943, 0.000221779694034276, 0.000210573319228928, 
0.000209814401954536, 0.000199563696531768, 0.000213700815397256, 
0.000206818574966683, 0.000208763397145302, 0.000209466888584875, 
0.000200561040212521, 0.00020736330151514, 0.000206615273552902, 
0.000226304936911876, 0.000191557225871038, 0.000209197257263542, 
0.000197196627459517, 0.000182648025073146, 0.000187167467334899, 
0.000207452753132798, 0.000255033338454542, 0.000204151641175841, 
0.000195517668420884, 0.000170845340017288, 0.000168421934585796, 
0.000184129784102488, 0.000193601477443027, 0.000175488936313069, 
0.000181060648778954, 0.000179620484958163, 0.000178424759504952, 
0.000181843, 0.000187026780254788, 0.000210843127188026, 0.000249750874216693, 
0.000184332753444335, 0.000263563989544533, 0.000221085293785622, 
0.00024050890337743, 0.000306076608905443, 0.000653290581453751, 
0.000259066949392466, 0.000217174578599705, 0.000206495476718441, 
0.000206588847382302, 0.000213913926304099, 0.00024780842574943, 
0.000231014667741643, 0.000214626673159216, 0.000706109577328312, 
0.000216572986312766, 0.000223600338999956, 0.000221478892778793, 
0.000263780671544593, 0.000225194301410931, 0.000345099948578436, 
0.000294012870562613, 0.000201918367158341, 0.000259107768792042, 
0.000203520383506092, 0.000208641473722905, 0.000204579207847345, 
0.0002003810978831, 0.000241168418749032, 0.000286534289195424, 
0.000222348536842653, 0.000218382835181926, 0.000182985527067606, 
0.000237105591842411, 0.000238807944850956, 0.000194129472636338, 
0.000233053859393739, 0.000194674758208508, 0.00017908049583222, 
0.000186322222360277, 0.000279918777020936, 0.000342408296692425, 
0.000188705, 0.000199185576642229, 0.000180363929150309, 0.000218275418502127, 
0.000176042262317206, 0.000192013383150881, 0.000223346729396081, 
0.000181962429016124, 0.000166604814938485, 0.000182101911051303, 
0.000221036301645668, 0.000205728429395956, 0.000219785203607917, 
0.000200442633420091, 0.000193995140598308, 0.000192136, 0.00022320584693451, 
0.00020231297255684, 0.000192004065142544, 0.000185996858893747, 
0.00019448035818312, 0.000192023203945828, 0.000198600381052749, 
0.000210295530669108, 0.000214508896446098, 0.000190450349813123, 
0.000181429310753587, 0.000193057947665284, 0.000190847391665553, 
0.000204111005200783, 0.000203891210038043, 0.000195470326234923, 
0.000193433402006726, 0.000227325576371516, 0.000196547567326979, 
0.000202819966842296, 0.000189682511264791, 0.000212201079972913, 
0.000178224229121272, 0.000176289242791267, 0.000194190033739972, 
0.00018774661141631, 0.00019266524291452, 0.000175939516478771, 
0.000185151474340347, 0.000195235086763546, 0.000189349353759849, 
0.00041044958421512, 0.000163802191585842, 0.000187317354359497, 
0.000193348661943357, 0.00019914411496206, 0.000267161610326919, 
0.000369227029344239, 0.000177143334755227, 0.000174668770613306, 
0.000188559068193631, 0.000177887399352389, 0.000175402040301172, 
0.000189531005281735, 0.000194339711765914, 0.000190365956134703, 
0.000221939857195154, 0.000281086095405424, 0.000198149818064018, 
0.000210196620349509, 0.000189518727110595, 0.000232423155689431, 
0.000177244823999531, 0.000186203624174478, 0.00017421055956467, 
0.000162068913865134, 0.000177692224706656, 0.000211733846768609, 
0.000179034504605524, 0.000179556656039021, 0.000177717963120753, 
0.000164561570130741, 0.00018329419608209, 0.000176863214209282, 
0.000201930622626055, 0.000222779955951516, 0.000209380084658741, 
0.000191084523247516, 0.000201371891497448, 0.000180369614659956, 
0.000202156933756671, 0.000170554321352654, 0.000187981896554783, 
0.000185847152574128, 0.00019056965756026, 0.000236116702702896, 
0.00016182560455468, 0.000215813106818604, 0.00018520364527866, 
0.000182226994925195, 0.000176894890756108, 0.000172883974659667, 
0.000177332553848883, 0.000176768563380539, 0.000184337592863557, 
0.000170796645236527, 0.000195951743423218, 0.000174544499226329, 
0.000180316002922458, 0.000188418885469255, 0.000185274, 0.000185274, 
0.000214575739041011, 0.000213649208627062, 0.000183362547403641, 
0.000184076644603146, 0.000272856229773424, 0.000232791942468667, 
0.000221392800982514, 0.000217146931181224, 0.000208563296570496, 
0.000219340468567567, 0.000207691013501049, 0.000195990363944023, 
0.000207477898268069, 0.000195533245112351, 0.000301628239011606, 
0.00020719750797164, 0.000225948974179557, 0.000175496338777332, 
0.00020291566153554, 0.000205642611720982, 0.000312898057643267, 
0.000178847725051191, 0.000202341179879732, 0.000209140317937077, 
0.000218939469993821, 0.000186842144328407, 0.00020019556582225, 
0.000180135211918095, 0.000197320881213445, 0.000191579369118776, 
0.000183293617827609, 0.000203158850158663, 0.000187933751585739, 
0.000193767406692145, 0.000202339470359058, 0.000234710546406956, 
0.000170139912804918, 0.000190024810598089, 0.000171576412429033, 
0.00018534986548068, 0.000173837664076311, 0.000169972312717937, 
0.000170154399489324, 0.000169574059399171, 0.000177967565209821, 
0.00017117695739335, 0.000157436793065761, 0.000165526023031951, 
0.000279210945039988, 0.000220824223002245, 0.000163289157307327, 
0.000161441861193925, 0.000163703935319692, 0.000168604182013255, 
0.000171227830520322, 0.000208621291173401, 0.000202704491142568, 
0.000229412646065061, 0.00021201792882327, 0.00018537138516772, 
0.000193666559654135, 0.000214135490714833, 0.000178551090160196, 
0.000206081814306913, 0.000170098208943123, 0.00019107372247764, 
0.000189245537514829, 0.000183124630384281, 0.000207769599648606, 
0.000182960386515571, 0.0002381590452259, 0.000163491324847107, 
0.000163999566565311, 0.000295724676285208, 0.000283142193324971, 
0.000181594613328427, 0.000225837417974964, 0.000171801108310484, 
0.000180783180859114, 0.000247613120838124, 0.000192467595309346, 
0.000171529250017353, 0.000177604176191318, 0.000166429780487406, 
0.000168774728391893, 0.000172944460230221, 0.00017155, 0.000157871277777064, 
0.000157096321623179, 0.000177292800539616, 0.000167217234649756, 
0.000169906492424345, 0.000180354017851647, 0.000161405725293211, 
0.000195893646571491, 0.000177296787638487, 0.000152159818447059, 
0.000164313558568904, 0.000159638632085939, 0.000155974885314132, 
0.000160058274744645, 0.000153161450529747, 0.000156700466774333, 
0.000164258428765945, 0.000162108847786387, 0.000161257, 0.000226731632213094, 
0.000181843, 0.000181843, 0.00020828682480877, 0.000183618579900381, 
0.000197117151201699, 0.000191169825491208, 0.00019346296144756, 
0.000192136, 0.000197871110143586, 0.000182464098045144, 0.000181843, 
0.000187193740716973, 0.000177689751121793, 0.000167718047449496, 
0.000168394115203756, 0.000186857003204341, 0.000170478117325094, 
0.000177300028562583, 0.000166356546043139, 0.000173430134655238, 
0.000177427848599225, 0.000172609694729693, 0.000177207576922291, 
0.000170467626621188, 0.000166542831768236, 0.000174519345193277, 
0.000170069991959935, 0.000172337527418408, 0.000175003256399374, 
0.000172078483102487, 0.000165546702103051, 0.000178626262382616, 
0.000184014145404535, 0.000175427001677552, 0.000180052546517096, 
0.000167736176338155, 0.000173880507786679, 0.000201784476880567, 
0.000205719066731234, 0.00017331803729319, 0.00015795818758561, 
0.00016424880981761, 0.000157593272744947, 0.000155756850865341, 
0.00016109612305558, 0.000159133123346858, 0.000157826, 0.000163561175417298, 
0.000169384433546243, 0.000166264884995174, 0.000191593787189162, 
0.000184716603905163, 0.00020512903025603, 0.000189079653422288, 
0.000176340381511451, 0.000186802209329016, 0.000195567, 0.000195567, 
0.000173469788153154, 0.000178057535334283, 0.000178153964221238, 
0.000181843, 0.000188189367953269, 0.000170351755700618, 0.000190870902996214, 
0.0001795980330427, 0.000183434492019468, 0.000178671405870098, 
0.000190720168900894, 0.000179794131992183, 0.00022825806989859, 
0.0001757505432049, 0.000188823283706877, 0.000211055504073154, 
0.000184405804895221, 0.000176334553930923, 0.000178825706559865, 
0.000188823711975162, 0.000173951368605241, 0.000192162975012509, 
0.00018059486850421, 0.000180077962343609, 0.000171681406487986, 
0.000176350889821388, 0.000175232832648959, 0.000186458228872848, 
0.000178317999421307, 0.000173535449290791, 0.000174773355766077, 
0.000172544239020556, 0.000194256209073896, 0.00017528586326849, 
0.00017155, 0.000172804082168421, 0.000175695248693669, 0.000177520794010836, 
0.000173256047847412, 0.000178025651671582, 0.000171699746488179, 
0.00016590500528343, 0.000184011350971719, 0.00017019888595757, 
0.000165689803471257, 0.000188569365570701, 0.000181229086963987, 
0.000181466497667667, 0.000183544231062122, 0.000518264683134071, 
0.000230866020804826, 0.000186587016637102, 0.000175580763207242, 
0.000177405709296225, 0.00017155, 0.000178685530646677, 0.000169169963900511, 
0.000182336721093659, 0.000175505391066844, 0.000177016506001358, 
0.00018543551560717, 0.000165116644534344, 0.000167340847154012, 
0.000179067931100113, 0.000171322769496991, 0.000174060887448484, 
0.000180414783890693, 0.00015528121691596, 0.000178029441961931, 
0.000161257, 0.000161257, 0.000165311526611936, 0.000218184796784865, 
0.000163386177877011, 0.00017569459223029, 0.000168464123437669, 
0.00016158516867365, 0.000182723813498859, 0.000170851107887945, 
0.000161557018164892, 0.000188658780619139, 0.000183608810439353, 
0.000173374638559402, 0.000176170669943069, 0.000155100256826046, 
0.000157206420481133, 0.00017904912557524, 0.000173878357045412, 
0.000200768886857161, 0.000184738377146169, 0.000159835985118121, 
0.000215767990866661, 0.000191326449499903, 0.000171631056904559, 
0.00017583172613254, 0.000200234559320349, 0.000175919870462586, 
0.000194794161890663, 0.000178675672785317, 0.000212298904280251, 
0.000184133740314431, 0.000185839833429475, 0.000193457631413125, 
0.000197950928429639, 0.00018251830830479, 0.000201357797628196, 
0.000176194871888045, 0.000179358322902374, 0.000186973521172994, 
0.000191996492658028, 0.000192466941471551, 0.000183866486237439, 
0.000192161521126876, 0.000207922650081327, 0.000174961450618972, 
0.000171951832654504, 0.000195152440329433, 0.000192822847002909, 
0.00016835080399074, 0.000178665363530427, 0.000181206637012515, 
0.000180272241805036, 0.000256880613444851, 0.00053164271961961, 
0.000181912107667204, 0.000182714001032788, 0.000213321462019315, 
0.000216153, 0.000164731860722123, 0.000178528005363527, 0.000176452421549634, 
0.000195567, 0.000157341891378892, 0.000198845548850578, 0.000162618285104318, 
0.00018764616958105, 0.000161959709571461, 0.000173324925503512, 
0.000462263706990745, 0.000231120951184785, 0.000158893869519493, 
0.000166442226555078, 0.000162062205916652, 0.000182820830162996, 
0.000168679523809757)), row.names = c(NA, -561L), class = "data.frame")
ggplot(plot_df,
       aes(x = Profile, y = depth_m, fill = Backscatter)) +
  geom_raster() +
  scale_y_reverse(limits = c(400, 0)) +
  scale_fill_viridis(
    option = "D",
    direction = -1,
    na.value = "white",
    limits = lims,
    oob = scales::squish) +
  labs(x = "Profile",
       y = "Depth (m)",
       fill = "Backscatter") +
  theme_minimal()

解决方案

ggplot2中的变换可以应用于任何刻度,不仅限于x/y。在这种情况,似乎只是对fill变量进行的一个简单幂变换。我最初也走到了 scales::exp_trans() 的路线上,可能是配合 viridis::,或将其替换为 scale_fill_gradientn(),但我认为 @JonSpring的建议使用 scales::pseudo_log_trans() 效果很好。

ggplot(small_df, aes(x=depth_m, y=depth_m, fill=Backscatter)) +
  geom_raster() +
  scale_y_reverse(limits = c(400, 0)) +
  labs(x="Depth (m)", y="Depth (m) again", fill = "Backscatter") +
  theme_minimal() + 
  viridis::scale_fill_viridis(
    trans = scales::pseudo_log_trans(sigma = 0.0001), # NEW
    limits = ~ range(.x, 0),                          # NEW, optional
    option = "D", na.value = "white", oob = scales::squish
  )

填充颜色的图例:刻度通过 scales 的伪对数变换进行了转换

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章