..level ..在ggplot :: stat_density2d中的含义是什么 [英] what does ..level.. mean in ggplot::stat_density2d
问题描述
我在构造一个将 fill
变量设置为 .. level ..
如下例所示:
library(MASS )
geg_density2d()+
stat_density2d(aes(fill = ..level ..),geom =polygon)
我怀疑。 .level ..
意味着 fill
被设置为当前图层的相对数量?也有人可以链接我一个很好的例子来解释这些二维密度图,每个轮廓代表什么等等?我在网上搜索,但找不到任何合适的指南。
扩展@hrbrmstr提供的答案 - 首先,调用 geom_density2d()
是多余的。也就是说,您可以通过以下方式获得相同的结果:
library(ggplot2)
library(MASS)
gg < - ggplot(geesser,aes(x = duration,y = waiting))+
geom_point()+
stat_density2d(aes(fill = ..level ..), geom =polygon)
让我们考虑其他一些方法来可视化这种密度估计,这可能有助于澄清什么:
base_plot< - ggplot(geyser,aes(x = duration,y = waiting))+
geom_point()
base_plot +
stat_density2d(aes(color = ..level ..))
base_plot +
stat_density2d (aes(fill = ..density ..),geom =raster,contour = FALSE)
base_plot +
stat_density2d (aes(alpha = ..density ..),geom =tile,contour = FALSE)
然而,我们再也看不到从 geom_point()
生成的点。
最后,请注意,您可以控制密度估算的带宽。为此,我们将 x
和 y
带宽参数传递给 h
(见?kde2d
):
base_plot +
stat_density2d(aes(fill = ..density ..),geom =raster,contour = FALSE,
h = c(2,5))
同样,从 geom_point ()
隐藏,因为它们在对 stat_density2d()
的调用之后。
I've seen some examples when constructing a heatmap of having the fill
variable set to ..level..
.
Such as in this example:
library(MASS)
ggplot(geyser, aes(x = duration, y = waiting)) +
geom_point() +
geom_density2d() +
stat_density2d(aes(fill = ..level..), geom = "polygon")
I suspect that the ..level..
means that the fill
is set to the relative amount of layers present? Also could someone link me a good example of how to interpret these 2D-density plots, what does each contour represent etc.? I have searched online but couldn't find any suitable guide.
Expanding on the answer provided by @hrbrmstr -- first, the call to geom_density2d()
is redundant. That is, you can achieve the same results with:
library(ggplot2)
library(MASS)
gg <- ggplot(geyser, aes(x = duration, y = waiting)) +
geom_point() +
stat_density2d(aes(fill = ..level..), geom = "polygon")
Let's consider some other ways to visualize this density estimate that may help clarify what is going on:
base_plot <- ggplot(geyser, aes(x = duration, y = waiting)) +
geom_point()
base_plot +
stat_density2d(aes(color = ..level..))
base_plot +
stat_density2d(aes(fill = ..density..), geom = "raster", contour = FALSE)
base_plot +
stat_density2d(aes(alpha = ..density..), geom = "tile", contour = FALSE)
Notice, however, we can no longer see the points generated from geom_point()
.
Finally, note that you can control the bandwidth of the density estimate. To do this, we pass x
and y
bandwidth arguments to h
(see ?kde2d
):
base_plot +
stat_density2d(aes(fill = ..density..), geom = "raster", contour = FALSE,
h = c(2, 5))
Again, the points from geom_point()
are hidden as they are behind the call to stat_density2d()
.
这篇关于..level ..在ggplot :: stat_density2d中的含义是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!