限制 viridis 颜色范围在两个值之间。

huangapple go评论69阅读模式
英文:

Restrict viridis colour scale to between two values

问题

I'd like to change the scale so that everything below 10 is black, then the colour scale is between e.g. 10 and 50, and then everything above 50 is the lightest yellow in the scale. In other words, I'd like to restrict the colour range to between 10 and 50. How can I do this?

library(ggplot2)
library(dplyr)
library(viridis)

dat %>%
  ggplot(aes(x=BP, fill=logp, y=1)) + 
  geom_tile() + 
  scale_fill_viridis(option='inferno') + 
  theme(axis.text = element_blank())
dat = structure(list(BP = structure(c(26L, 33L, 24L, 34L, 36L, 54L,
58L, 43L, 7L, 36L, 36L, 16L, 6L, 55L, 40L, 27L, 26L, 22L, 6L,
26L, 15L, 7L, 43L, 6L, 5L, 49L, 20L, 45L, 60L, 26L, 7L, 55L,
40L, 23L, 46L, 18L, 48L, 29L, 28L, 63L, 30L, 57L, 6L, 50L, 18L,
54L, 5L, 12L, 6L, 7L, 57L, 32L, 59L, 33L, 2L, 33L, 37L, 9L, 23L,
20L, 54L, 57L, 45L, 42L, 16L, 55L, 35L, 28L, 60L, 61L, 9L, 48L,
63L, 28L, 10L, 11L, 34L, 21L, 62L, 39L, 55L, 32L, 60L, 29L, 62L,
38L, 49L, 50L, 28L, 16L, 18L, 59L, 1L, 40L, 60L, 58L, 19L, 60L,
58L, 57L), .Label = c("29910359", "29910371", "29910558", "29910582",
"29910588", "29910663", "29910699", "29910717", "29910720", "29910726",
"29910729", "29910732", "29910741", "29910750", "29910753", "29910759",
"29910762", "29910768", "29910771", "29910774", "29910777", "29910780",
"29910801", "29911057", "29911063", "29911069", "29911078", "29911087",
"29911093", "29911099", "29911114", "29911120", "29911153", "29911198",
"29911204", "29911207", "29911219", "29911222", "29911225", "29911228",
"29911240", "29911246", "29911255", "29911261", "29911270", "29911273",
"29911903", "29911930", "29911933", "29911972", "29912086", "29912089",
"29912110", "29912281", "29912299", "29912335", "29912344", "29912347",
"29912350", "29912374", "29912386", "29912858", "29913039", "31237131",
"31237170", "31237773", "31237776", "31237779", "31237782", "31237785",
"31237809", "31237839", "31237869", "31237996", "31238014", "31238032",
"31238056", "31238071", "31238158", "31238182", "31238233", "31238263",
"31238871", "31238877", "31238883", "31238913", "31238934", "31238946",
"31238961", "31238988", "31239054", "31239060", "31239063", "31239093",
"31239105", "31239111", "31239117", "31239120", "31239129", "31239378",
"31239408", "31239417", "312394

<details>
<summary>英文:</summary>

I&#39;d like to change the scale so that everything below 10 is black, then the colour scale is between e.g. 10 and 50, and then everything above 50 is the lightest yellow in the scale. In other words, I&#39;d like to restrict the colour range to between 10 and 50. How can I do this?


[![enter image description here][1]][1]


library(ggplot2)
library(dplyr)
library(viridis)

dat %>%
ggplot(aes(x=BP, fill=logp, y=1)) +
geom_tile() +
scale_fill_viridis(option='inferno') +
theme(axis.text = element_blank())


dat = structure(list(BP = structure(c(26L, 33L, 24L, 34L, 36L, 54L,
58L, 43L, 7L, 36L, 36L, 16L, 6L, 55L, 40L, 27L, 26L, 22L, 6L,
26L, 15L, 7L, 43L, 6L, 5L, 49L, 20L, 45L, 60L, 26L, 7L, 55L,
40L, 23L, 46L, 18L, 48L, 29L, 28L, 63L, 30L, 57L, 6L, 50L, 18L,
54L, 5L, 12L, 6L, 7L, 57L, 32L, 59L, 33L, 2L, 33L, 37L, 9L, 23L,
20L, 54L, 57L, 45L, 42L, 16L, 55L, 35L, 28L, 60L, 61L, 9L, 48L,
63L, 28L, 10L, 11L, 34L, 21L, 62L, 39L, 55L, 32L, 60L, 29L, 62L,
38L, 49L, 50L, 28L, 16L, 18L, 59L, 1L, 40L, 60L, 58L, 19L, 60L,
58L, 57L), .Label = c("29910359", "29910371", "29910558", "29910582",
"29910588", "29910663", "29910699", "29910717", "29910720", "29910726",
"29910729", "29910732", "29910741", "29910750", "29910753", "29910759",
"29910762", "29910768", "29910771", "29910774", "29910777", "29910780",
"29910801", "29911057", "29911063", "29911069", "29911078", "29911087",
"29911093", "29911099", "29911114", "29911120", "29911153", "29911198",
"29911204", "29911207", "29911219", "29911222", "29911225", "29911228",
"29911240", "29911246", "29911255", "29911261", "29911270", "29911273",
"29911903", "29911930", "29911933", "29911972", "29912086", "29912089",
"29912110", "29912281", "29912299", "29912335", "29912344", "29912347",
"29912350", "29912374", "29912386", "29912858", "29913039", "31237131",
"31237170", "31237773", "31237776", "31237779", "31237782", "31237785",
"31237809", "31237839", "31237869", "31237996", "31238014", "31238032",
"31238056", "31238071", "31238158", "31238182", "31238233", "31238263",
"31238871", "31238877", "31238883", "31238913", "31238934", "31238946",
"31238961", "31238988", "31239054", "31239060", "31239063", "31239093",
"31239105", "31239111", "31239117", "31239120", "31239129", "31239378",
"31239408", "31239417", "31239429", "31239450", "31239621", "31239630",
"31239645", "31239802", "31239820", "31239826", "31322303", "31322910",
"31322979", "31323183", "31323285", "31323321", "31323336", "31323952",
"31323958", "31323961", "31323979", "31323991", "31324003", "31324018",
"31324024", "31324036", "31324051", "31324057", "31324063", "31324099",
"31324144", "31324150", "31324153", "31324183", "31324195", "31324201",
"31324207", "31324210", "31324488", "31324491", "31324497", "31324506",
"31324509", "31324515", "31324524", "31324527", "31324530", "31324536",
"31324539", "31324542", "31324548", "31324551", "31324581", "31324599",
"31324602", "31324614", "31324641", "31324665", "31324710", "31324886",
"31324892", "31324895", "31324910", "31324925", "31324931", "32548048",
"32548054", "32548667", "32549392", "32549395", "32549437", "32549488",
"32549509", "32549515", "32549536", "32549575", "32549599", "32549623",
"32549641", "32549647", "32551904", "32551907", "32551928", "32551931",
"32551940", "32551943", "32551949", "32551952", "32551961", "32551982",
"32551988", "32551991", "32552021", "32552042", "32552048", "32552051",
"32552063", "32552066", "32552069", "32552072", "32552078", "32552084",
"32552087", "32552114", "32552120", "32552123", "32552126", "32552129",
"32552132", "32552135", "32557422", "32557434", "32557479", "32557482",
"32557503", "32557506", "32605258", "32609219", "32609228", "32609234",
"32609237", "32609240", "32609243", "32609246", "32609261", "32609270",
"32609276", "32609285", "32609303", "32609306", "32609318", "32609812",
"32609878", "32609881", "32609959", "32609974", "32609980", "32610016",
"32610410", "32610470", "32610494", "32610503", "32627649", "32628263",
"32628266", "32628269", "32628272", "32628275", "32628278", "32628281",
"32628775", "32628784", "32628787", "32628838", "32628856", "32629408",
"32629417", "32629462", "32629543", "32629573", "32629585", "32629588",
"32629615", "32632585", "32632591", "32632594", "32632630", "32632639",
"32632642", "32632651", "32632681", "32632687", "32632741", "32632762",
"32632774", "32632810", "32632825", "32634281", "32634299", "32634302",
"32634305", "32634314", "32634317", "32634338", "32634341", "32634350",
"32634368", "33036434", "33036548", "33036852", "33036951", "33036999",
"33037044", "33037639", "33048466", "33048544", "33048604", "33048634",
"33048646", "33048667", "33048691", "33052678", "33052900", "33052924",
"33053552"), class = "factor"), logp = c(172.431898976787, 26.7857587447667,
13.7182639609103, 4.59597437859162, 3.37135602963993, 2.42203849224475,
2.33614140334707, 2.27342522817656, 2.12279565347012, 1.95493268607045,
1.84162860002246, 1.76250583869454, 1.3462872698409, 1.33451594631893,
1.32057094957627, 1.26432557380829, 1.25905775385668, 1.22220865943337,
1.18495823854813, 1.14232906187613, 1.12151773814716, 1.05698338230305,
1.02356162817423, 0.999962760117936, 0.914947243696157, 0.901303708786404,
0.825620710573798, 0.800442427984661, 0.77960549453314, 0.770418368816999,
0.71440368181689, 0.672394925414033, 0.627569540838043, 0.626008921707965,
0.618615151098098, 0.606647998000934, 0.605007357807543, 0.579685802380332,
0.570056661439807, 0.564793890130562, 0.531164152438947, 0.52651735508726,
0.506542489156532, 0.503624407482339, 0.502104206545705, 0.500113054531826,
0.499461302018343, 0.475504707126549, 0.46350295195749, 0.460304276908432,
0.45980691663331, 0.439923847258054, 0.433725953037076, 0.417020086882588,
0.411557565702582, 0.390839307101091, 0.389214495590406, 0.387562209514802,
0.38537221902263, 0.385054197773502, 0.354198993470542, 0.338125815163842,
0.331813530700858, 0.322716044510619, 0.312407660157186, 0.302642403361189,
0.275546460481729, 0.27003861681199, 0.266170828564881, 0.247208307401447,
0.216958685104683, 0.215907481935428, 0.215874747881349, 0.200828919694514,
0.182043763547403, 0.151201020930873, 0.150589897124663, 0.120648979017238,
0.0923331643431691, 0.0876061326642202, 0.0852037912848987, 0.081926140677378,
0.0723683490310392, 0.0694239863867504, 0.0619533015953892, 0.060598649814778,
0.056585234408231, 0.0435363425453365, 0.0430611683159134, 0.0426892484518421,
0.0383953680479647, 0.0352425623437869, 0.0332852606530205, 0.0293031922333685,
0.0198856082863035, 0.0179743082673358, 0.0149572476809299, 0.010457417968552,
0.00493974392637725, 0.00376729022205555)), row.names = c(NA,
-100L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x5609bb72e640>)
> dput(as.data.frame(dat))
structure(list(BP = structure(c(26L, 33L, 24L, 34L, 36L, 54L,
58L, 43L, 7L, 36L, 36L, 16L, 6L, 55L, 40L, 27L, 26L, 22L, 6L,
26L, 15L, 7L, 43L, 6L, 5L, 49L, 20L, 45L, 60L, 26L, 7L, 55L,
40L, 23L, 46L, 18L, 48L, 29L, 28L, 63L, 30L, 57L, 6L, 50L, 18L,
54L, 5L, 12L, 6L, 7L, 57L, 32L, 59L, 33L, 2L, 33L, 37L, 9L, 23L,
20L, 54L, 57L, 45L, 42L, 16L, 55L, 35L, 28L, 60L, 61L, 9L, 48L,
63L, 28L, 10L, 11L, 34L, 21L, 62L, 39L, 55L, 32L, 60L, 29L, 62L,
38L, 49L, 50L, 28L, 16L, 18L, 59L, 1L, 40L, 60L, 58L, 19L, 60L,
58L, 57L), .Label = c("29910359", "29910371", "29910558", "29910582",
"29910588", "29910663", "29910699", "29910717", "29910720", "29910726",
"29910729", "29910732", "29910741", "29910750", "29910753", "29910759",
"29910762", "29910768", "29910771", "29910774", "29910777", "29910780",
"29910801", "29911057", "29911063", "29911069", "29911078", "29911087",
"29911093", "29911099", "29911114", "29911120", "29911153", "29911198",
"29911204", "29911207", "29911219", "29911222", "29911225", "29911228",
"29911240", "29911246", "29911255", "29911261", "29911270", "29911273",
"29911903", "29911930", "29911933", "29911972", "29912086", "29912089",
"29912110", "29912281", "29912299", "29912335", "29912344", "29912347",
"29912350", "29912374", "29912386", "29912858", "29913039", "31237131",
"31237170", "31237773", "31237776", "31237779", "31237782", "31237785",
"31237809", "31237839", "31237869", "31237996", "31238014", "31238032",
"31238056", "31238071", "31238158", "31238182", "31238233", "31238263",
"31238871", "31238877", "31238883", "31238913", "31238934", "31238946",
"31238961", "31238988", "31239054", "31239060", "31239063", "31239093",
"31239105", "31239111", "31239117", "31239120", "31239129", "31239378",
"31239408", "31239417", "31239429", "31239450", "31239621", "31239630",
"31239645", "31239802", "31239820", "31239826", "31322303", "31322910",
"31322979", "31323183", "31323285", "31323321", "31323336", "31323952",
"31323958", "31323961", "31323979", "31323991", "31324003", "31324018",
"31324024", "31324036", "31324051", "31324057", "31324063", "31324099",
"31324144", "31324150", "31324153", "31324183", "31324195", "31324201",
"31324207", "31324210", "31324488", "31324491", "31324497", "31324506",
"31324509", "31324515", "31324524", "31324527", "31324530", "31324536",
"31324539", "31324542", "31324548", "31324551", "31324581", "31324599",
"31324602", "31324614", "31324641", "31324665", "31324710", "31324886",
"31324892", "31324895", "31324910", "31324925", "31324931", "32548048",
"32548054", "32548667", "32549392", "32549395", "32549437", "32549488",
"32549509", "32549515", "32549536", "32549575", "32549599", "32549623",
"32549641", "32549647", "32551904", "32551907", "32551928", "32551931",
"32551940", "32551943", "32551949", "32551952", "32551961", "32551982",
"32551988", "32551991", "32552021", "32552042", "32552048", "32552051",
"32552063", "32552066", "32552069", "32552072", "32552078", "32552084",
"32552087", "32552114", "32552120", "32552123", "32552126", "32552129",
"32552132", "32552135", "32557422", "32557434", "32557479", "32557482",
"32557503", "32557506", "32605258", "32609219", "32609228", "32609234",
"32609237", "32609240", "32609243", "32609246", "32609261", "32609270",
"32609276", "32609285", "32609303", "32609306", "32609318", "32609812",
"32609878", "32609881", "32609959", "32609974", "32609980", "32610016",
"32610410", "32610470", "32610494", "32610503", "32627649", "32628263",
"32628266", "32628269", "32628272", "32628275", "32628278", "32628281",
"32628775", "32628784", "32628787", "32628838", "32628856", "32629408",
"32629417", "32629462", "32629543", "32629573", "32629585", "32629588",
"32629615", "32632585", "32632591", "32632594", "32632630", "32632639",
"32632642", "32632651", "32632681", "32632687", "32632741", "32632762",
"32632774", "32632810", "32632825", "32634281", "32634299", "32634302",
"32634305", "32634314", "32634317", "32634338", "32634341", "32634350",
"32634368", "33036434", "33036548", "33036852", "33036951", "33036999",
"33037044", "33037639", "33048466", "33048544", "33048604", "33048634",
"33048646", "33048667", "33048691", "33052678", "33052900", "33052924",
"33053552"), class = "factor"), logp = c(172.431898976787, 26.7857587447667,
13.7182639609103, 4.59597437859162, 3.37135602963993, 2.42203849224475,
2.33614140334707, 2.27342522817656, 2.12279565347012, 1.95493268607045,
1.84162860002246, 1.76250583869454, 1.3462872698409, 1.33451594631893,
1.32057094957627, 1.26432557380829, 1.25905775385668, 1.22220865943337,
1.18495823854813, 1.14232906187613, 1.12151773814716, 1.05698338230305,
1.02356162817423, 0.999962760117936, 0.914947243696157, 0.901303708786404,
0.825620710573798, 0.800442427984661, 0.77960549453314, 0.770418368816999,
0.71440368181689, 0.672394925414033, 0.627569540838043, 0.626008921707965,
0.618615151098098, 0.606647998000934, 0.605007357807543, 0.579685802380332,
0.570056661439807, 0.564793890130562, 0.531164152438947, 0.52651735508726,
0.506542489156532, 0.503624407482339, 0.502104206545705, 0.500113054531826,
0.499461302018343, 0.475504707126549, 0.46350295195749, 0.460304276908432,
0.45980691663331, 0.439923847258054, 0.433725953037076, 0.417020086882588,
0.411557565702582, 0.390839307101091, 0.389214495590406, 0.387562209514802,
0.38537221902263, 0.385054197773502, 0.354198993470542, 0.338125815163842,
0.331813530700858, 0.322716044510619, 0.312407660157186, 0.302642403361189,
0.275546460481729, 0.27003861681199, 0.266170828564881, 0.247208307401447,
0.216958685104683, 0.215907481935428, 0.215874747881349, 0.200828919694514,
0.182043763547403, 0.151201020930873, 0.150589897124663, 0.120648979017238,
0.0923331643431691, 0.0876061326642202, 0.0852037912848987, 0.081926140677378,
0.0723683490310392, 0.0694239863867504, 0.0619533015953892, 0.060598649814778,
0.056585234408231, 0.0435363425453365, 0.0430611683159134, 0.0426892484518421,
0.0383953680479647, 0.0352425623437869, 0.0332852606530205, 0.0293031922333685,
0.0198856082863035, 0.0179743082673358, 0.0149572476809299, 0.010457417968552,
0.00493974392637725, 0.00376729022205555)), row.names = c(NA,
-100L), class = "data.frame")


[1]: https://i.stack.imgur.com/9nbyV.png
</details>
# 答案1
**得分**: 3
这里是如何调整Claus Wilke的答案(https://stackoverflow.com/a/48429657/5784757)的方法。您需要创建一个函数将您的数据范围映射到(0,1),然后将其传递给`rescaler`参数。
```R
myscaler <- function(x, from, to) {
high = 50
low = 10
ifelse(x < low, 0, ifelse(x > high, 1, (x - low) / (high - low)))
}
dat %>%
ggplot(aes(x = BP, fill = logp, y = 1)) + 
geom_tile() + 
scale_fill_viridis_c(option = 'inferno', rescaler = myscaler) + 
theme(axis.text = element_blank())
英文:

Here's how to adapt Claus Wilke's answer (https://stackoverflow.com/a/48429657/5784757). You need to make a function to map your data range to (0,1) then you pass that to the rescaler argument.

myscaler &lt;- function(x, from, to) {
high=50
low=10
ifelse(x&lt;low,0,ifelse(x&gt;high,1,(x-low)/(high-low)))
}
dat %&gt;% 
ggplot(aes(x=BP, fill=logp, y=1)) + 
geom_tile() + 
scale_fill_viridis_c(option=&#39;inferno&#39; , rescaler=myscaler) + 
theme(axis.text = element_blank())

答案2

得分: 0

你可以尝试使用以下代码部分:scale_fill_viridis(option=&#39;inferno&#39;, limits = c(10, 50), oob = scales::oob_squish)

英文:

You can try scale_fill_viridis(option=&#39;inferno&#39;, limits = c(10, 50), oob = scales::oob_squish)

huangapple
  • 本文由 发表于 2023年5月17日 18:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定