简体   繁体   English

如何删除 stat_density_2d 中的背景?

[英]How can I delete the background in stat_density_2d?

I am trying to make density maps in R using the function stat_density_2d, but I would like to remove the background color for which the density is null. I tried changing the limits for the density, but when moving the limits from [0,5] to [0.1, 5], the background becomes grey instead of dark blue.我正在尝试使用 function stat_density_2d 在 R 中制作密度图,但我想删除密度为 null 的背景颜色。我尝试更改密度的限制,但是当从 [0,5] 移动限制时到 [0.1, 5],背景变成灰色而不是深蓝色。 What can I do to have a transparent background, and colouring only the datapoints?我该怎么做才能拥有透明背景并仅为数据点着色?

Here is my code:这是我的代码:

ggplot(TEST, aes(x = X, y = Y)) +
  geom_point() +
  stat_density_2d(geom = "raster", aes(fill = ..density..*10e04), contour = F, 
                  h = c(5, 5),
                  n = 300) +
  ggtitle("7387")+ 
  theme(plot.title = element_text(lineheight=.8, face="bold"))+
  scale_y_reverse()+
  scale_fill_distiller(palette = 'Spectral', limits=c(0,5))

在此处输入图像描述

Thank you!谢谢你!

You could map the density to the alpha scale so that lower values are transparent:您可以将密度映射到 alpha 比例,以便较低的值是透明的:

TEST <- data.frame(X = rnorm(10000, -700, 50),
                   Y = rnorm(10000, -450, 50))

ggplot(TEST, aes(x = X, y = Y)) +
  stat_density_2d(geom = "raster", 
                  aes(fill = ..density..*10e04, alpha = ..density..*10e04), 
                  contour = FALSE, 
                  h = c(7, 7),
                  n = 300) +
  scale_alpha_continuous(range = c(0, 1), limits = c(0, 3), 
                         guide = guide_none()) +
  ggtitle("7387") + 
  theme(plot.title = element_text(lineheight=.8, face="bold"))+
  scale_y_reverse() +
  scale_fill_viridis_c(limits = c(0, 12))

在此处输入图像描述

Building on the accepted answer -- the background goes grey when setting the limits because coordinates falling outside the limits receive a value of NA , and the default fill for NA is "grey50" .基于公认的答案 - 设置限制时背景变为灰色,因为超出限制的坐标接收值NA ,并且NA的默认填充为"grey50" To use a different color, specify na.value .要使用不同的颜色,请指定na.value For removing the background, set the lower limit to some value near 0 and then use "transparent"要去除背景,请将下限设置为接近 0 的某个值,然后使用"transparent"

library(ggplot2)

TEST <- data.frame(X = rnorm(10000, -700, 50),
                   Y = rnorm(10000, -450, 50))

ggplot(TEST, aes(x = X, y = Y)) +
  stat_density_2d(geom = "raster", 
                  aes(fill = ..density..*10e04), 
                  contour = FALSE, 
                  h = c(7, 7),
                  n = 300) +
  ggtitle("7387") + 
  theme(plot.title = element_text(lineheight=.8, face="bold"))+
  scale_y_reverse() +
  scale_fill_viridis_c(limits = c(0.001, 12), na.value = "transparent")

Created on 2022-05-22 by the reprex package (v2.0.1)reprex 包于 2022-05-22 创建 (v2.0.1)

I'd like to propose another possible solution here.我想在这里提出另一种可能的解决方案。 The prior solutions either require you to use an alpha scale or require you to hard code some values that are relative to the range of your data.先前的解决方案要么要求您使用 alpha 标度,要么要求您硬编码一些与数据范围相关的值。 The alpha scale is hard to get right if you really only want the low values to be transparent but not the mid/high.如果您真的只希望低值透明而不是中/高值,则很难获得正确的 alpha 比例。 The hardcoding is problematic if you are making multiple graphs or using different inputs (eg a lower bound of.01 might look good on one dataset, but cut off important values on another).如果您正在制作多个图表或使用不同的输入(例如,.01 的下限在一个数据集上可能看起来不错,但在另一个数据集上会切断重要值),则硬编码会有问题。

The solution is to create a custom color map that begins with 'transparent' .解决方案是创建以'transparent'开头的自定义颜色 map。 You can do this using existing color ramps like viridis.您可以使用现有的色带(如 viridis)来执行此操作。

Calling viridis::viridis_pal()(n) will give you a vector of colors from the viridis scale.调用viridis::viridis_pal()(n)将为您提供来自 viridis 标度的向量 colors。 You can also access others with something like viridis::viridis_pal(option='magma')(10) which will you give you 10 colors from the range of the magma palette.您还可以使用viridis::viridis_pal(option='magma')(10)类的内容访问其他内容,这将为您提供岩浆调色板范围内的 10 colors。

To create your new color ramp, use scale_fill_gradientn() and concatenate 'transparent' in front of a range of colors from your desired scale:要创建新的色带,请使用scale_fill_gradientn()并在所需比例的 colors 范围前面连接'transparent'

scale_fill_gradientn(colors = c('transparent',viridis::viridis_pal()(10)))

Now you have a scale that is transparent on the smallest end of the range but then proceeds through the viridis color ramp.现在你有一个在范围的最小端是透明的比例,但随后通过 viridis 颜色渐变。 You can also choose your own colors here as desired rather than relying on a built-in scale.您也可以根据需要在此处选择您自己的 colors 而不是依赖内置秤。

TEST <- data.frame(X = rnorm(10000, -700, 50),
                   Y = rnorm(10000, -450, 50))

ggplot(TEST, aes(x = X, y = Y)) +
  stat_density_2d(geom = "raster", aes(fill = ..density..*10e04), contour = F, 
                  h = c(5, 5),
                  n = 300) +
  ggtitle("7387")+ 
  theme(plot.title = element_text(lineheight=.8, face="bold"))+
  scale_y_reverse()+
  scale_fill_gradientn(colors = c('transparent',viridis::viridis_pal()(10)))

n=10 的示例

You can also attenuate this effect by adjusting how many colors you use to build your ramp.您还可以通过调整用于构建斜坡的 colors 的数量来减弱这种影响。 The effect is subtle, but more colors means more of the "background" is filled in, while fewer colors will allow more of the background to fade away.效果很微妙,但更多的 colors 意味着更多的“背景”被填充,而更少的 colors 将允许更多的背景淡出。 The below compares viridis::viridis_pal()(5) and viridis::viridis_pal()(30) :下面比较viridis::viridis_pal()(5)viridis::viridis_pal()(30)

在此处输入图像描述

This effect is independent of the scaling of your data--if you multiply density by 10, the result will be identical (which it would not be if you use the methods based on hard-coded limits).这种效果与数据的缩放无关——如果将密度乘以 10,结果将相同(如果使用基于硬编码限制的方法则不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM