如何删除 stat_density_2d 中的背景?
How can I delete the background in stat_density_2d?
我正在尝试使用函数 stat_density_2d 在 R 中制作密度图,但我想删除密度为空的背景颜色。我尝试更改密度的限制,但是当将限制从 [0,5] 移动到 [0.1, 5] 时,背景变成灰色而不是深蓝色。我该怎么做才能拥有透明背景并仅对数据点着色?
这是我的代码:
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))
谢谢!
您可以将密度映射到 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))
基于已接受的答案——设置限制时背景变灰,因为超出限制的坐标接收值 NA
,NA
的默认填充为 "grey50"
.要使用不同的颜色,请指定 na.value
。要删除背景,请将下限设置为接近 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")
由 reprex package (v2.0.1)
创建于 2022-05-22
我正在尝试使用函数 stat_density_2d 在 R 中制作密度图,但我想删除密度为空的背景颜色。我尝试更改密度的限制,但是当将限制从 [0,5] 移动到 [0.1, 5] 时,背景变成灰色而不是深蓝色。我该怎么做才能拥有透明背景并仅对数据点着色?
这是我的代码:
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))
谢谢!
您可以将密度映射到 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))
基于已接受的答案——设置限制时背景变灰,因为超出限制的坐标接收值 NA
,NA
的默认填充为 "grey50"
.要使用不同的颜色,请指定 na.value
。要删除背景,请将下限设置为接近 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")
由 reprex package (v2.0.1)
创建于 2022-05-22