如何在 R 中的梯度刻度中创建值间隙

发布于 2025-01-13 12:55:58 字数 653 浏览 1 评论 0原文

我目前正在创建一张美国地图,其中包含每个州内变量(“totalvisits”)计数的梯度。

有一个州的计数非常高(即,MA - 超过 8000),而其他所有州都小于 500。我是否可以在渐变比例中创建一个间隙,以便跳过 500 - 8000 之间的值。我还希望有一些东西可以更好地可视化较低值状态之间的差异,因为现在热图使它看起来它们都具有相同的值(下图)

这是我当前的代码:

statebins_US <- 
  statebins(state_data = patient_origin_bystate, state_col = "region", value_col = "totalvisits", dark_label = "black", light_label = "white", font_size = 3) + 
  theme_statebins(legend_position="right") + 
  scale_fill_gradientn(colours = rev(brewer.pal(10, "Spectral")), na.value = "gray78") 
 

当前热图 - 较低值的状态均具有相同的颜色

I am currently creating a map of the U.S. with a gradient of counts of a variable ('totalvisits') within each state.

There is one state that has really high count (i.e., MA - over 8000) while every other state are <500. Would it be possible for me to create a gap in the gradient scale such that values from 500 - 8000 are skipped. I'd also like to have something where the differences between states of lower values can be better visualized, because right now the heat map makes it look like they all have the same value (picture below)

This is my current code:

statebins_US <- 
  statebins(state_data = patient_origin_bystate, state_col = "region", value_col = "totalvisits", dark_label = "black", light_label = "white", font_size = 3) + 
  theme_statebins(legend_position="right") + 
  scale_fill_gradientn(colours = rev(brewer.pal(10, "Spectral")), na.value = "gray78") 
 

Current Heat Map - Where States of Lower Values all have Same Color

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所有深爱都是秘密 2025-01-20 12:55:58

我不会回答你的问题,而是从另一个方向提出建议。如果您的值都是严格正数,我建议您改用对数转换。 (如果您的值包含负值或 0,您可以使用伪对数变换)

这是您的问题的简短模型,其中异常值很少。

library(ggplot2)

set.seed(0)
df <- expand.grid(LETTERS[1:6], LETTERS[1:6])
df$value <- rlnorm(nrow(df), meanlog = 6)

p <- ggplot(df, aes(Var1, Var2)) +
  geom_tile(aes(fill = value))

p + scale_fill_distiller(palette = "Spectral")

我的建议是使用对数转换的调色板,而不是不连续的调色板。在这种情况下,应该使用更多种类的调色板颜色。

p + scale_fill_distiller(palette = "Spectral", trans = "log10")

reprex 包 (v2.0.1)

I am not going to answer your question, but make a suggestion in another direction. If your values are all strictly positive, I'm going to recommend that you use a log-transformation instead. (You could use a pseudo-log transform if your values include negative values or 0)

Here is a brief mock-up of your problem, where you have very few outlier values.

library(ggplot2)

set.seed(0)
df <- expand.grid(LETTERS[1:6], LETTERS[1:6])
df$value <- rlnorm(nrow(df), meanlog = 6)

p <- ggplot(df, aes(Var1, Var2)) +
  geom_tile(aes(fill = value))

p + scale_fill_distiller(palette = "Spectral")

My recommendation is to use a log transformed colour palette, instead of a discontinuous palette. In such cases it should use a larger variety of colours of the palette.

p + scale_fill_distiller(palette = "Spectral", trans = "log10")

Created on 2022-03-10 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文