Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R stat_density2d-图例是什么意思?_R_Stat Density2d - Fatal编程技术网

R stat_density2d-图例是什么意思?

R stat_density2d-图例是什么意思?,r,stat-density2d,R,Stat Density2d,我在R中用stat\u density2d绘制了一张地图。代码如下: ggplot(data, aes(x=Lon, y=Lat)) + stat_density2d(aes(fill = ..level..), alpha=0.5, geom="polygon",show.legend=FALSE)+ geom_point(colour="red")+ geom_path(data=map.df,aes(x=long, y=lat, group=group), colour="g

我在R中用
stat\u density2d
绘制了一张地图。代码如下:

ggplot(data, aes(x=Lon, y=Lat)) + 
  stat_density2d(aes(fill = ..level..), alpha=0.5, geom="polygon",show.legend=FALSE)+
  geom_point(colour="red")+
  geom_path(data=map.df,aes(x=long, y=lat, group=group), colour="grey50")+
  scale_fill_gradientn(colours=rev(brewer.pal(7,"Spectral")))+
  xlim(-10,+2.5) +
  ylim(+47,+60) +
  coord_fixed(1.7) +
  theme_void()
它产生了这样的结果:

太好了。它起作用了。但是我不知道这个传说是什么意思。我确实找到了这个维基百科页面:

他们使用的例子(包括红色、橙色和黄色)说明:

彩色轮廓对应于包含以下内容的最小区域: 各自的概率质量:红色=25%,橙色+红色=50%,黄色 +橙色+红色=75%

然而,使用stat_density2d,我的地图中有11条等高线。有人知道stat_density2d是如何工作的吗?这个传说意味着什么?理想情况下,我希望能够陈述一些东西,比如红色轮廓包含25%的绘图等


我读过这篇文章:但我仍然一点也不聪明。

让我们以ggplot2中忠实的
为例:

ggplot(faithful, aes(x = eruptions, y = waiting)) +
  stat_density_2d(aes(fill = factor(stat(level))), geom = "polygon") +
  geom_point() +
  xlim(0.5, 6) +
  ylim(40, 110)
(为没能让这个更漂亮提前道歉)

标高是三维“山脉”被切割的高度。我不知道如何(其他人可能)将其转化为一个百分比,但我知道如何让你得到所说的百分比

如果我们查看该图表,级别
0.002
包含绝大多数点(除2点外)。Level
0.004
实际上是两个多边形,它们包含除十几个点以外的所有点。如果我得到了你要问的要点,那就是你想知道的,除了不计算,而是给定水平上多边形所包含的点的百分比。使用所涉及的各种ggplot2“统计数据”中的方法进行计算非常简单

请注意,在导入
tidyverse
sp
软件包时,我们将使用一些其他完全限定的函数。现在,让我们稍微重塑一下
忠实的
数据:

library(tidyverse)
library(sp)

xdf <- select(faithful, x = eruptions, y = waiting)
我不会用
str()
output把答案弄得乱七八糟,但是看看那里发生了什么是很有趣的

我们可以使用空间运算来计算给定多边形内的点数,然后我们可以在同一级别对多边形进行分组,以提供每个级别的计数和百分比:

SpatialPolygons(
  lapply(1:length(cl), function(idx) {
    Polygons(
      srl = list(Polygon(
        matrix(c(cl[[idx]]$x, cl[[idx]]$y), nrow=length(cl[[idx]]$x), byrow=FALSE)
      )),
      ID = idx
    )
  })
) -> cont

coordinates(xdf) <- ~x+y

data_frame(
  ct = sapply(over(cont, geometry(xdf), returnList = TRUE), length),
  id = 1:length(ct),
  lvl = sapply(cl, function(x) x$level)
) %>% 
  count(lvl, wt=ct) %>% 
  mutate(
    pct = n/length(xdf),
    pct_lab = sprintf("%s of the points fall within this level", scales::percent(pct))
  )
## # A tibble: 12 x 4
##      lvl     n    pct pct_lab                              
##    <dbl> <int>  <dbl> <chr>                                
##  1 0.002   270 0.993  99.3% of the points fall within this level
##  2 0.004   259 0.952  95.2% of the points fall within this level
##  3 0.006   249 0.915  91.5% of the points fall within this level
##  4 0.008   232 0.853  85.3% of the points fall within this level
##  5 0.01    206 0.757  75.7% of the points fall within this level
##  6 0.012   175 0.643  64.3% of the points fall within this level
##  7 0.014   145 0.533  53.3% of the points fall within this level
##  8 0.016    94 0.346  34.6% of the points fall within this level
##  9 0.018    81 0.298  29.8% of the points fall within this level
## 10 0.02     60 0.221  22.1% of the points fall within this level
## 11 0.022    43 0.158  15.8% of the points fall within this level
## 12 0.024    13 0.0478  4.8% of the points fall within this level 
空间多边形(
搭接(1:长度(cl),函数(idx){
多边形(
srl=列表(多边形)(
矩阵(c(cl[[idx]]$x,cl[[idx]]$y),nrow=长度(cl[[idx]]]$x),byrow=假)
)),
ID=idx
)
})
)->续
坐标(xdf)%
计数(lvl,wt=ct)%>%
变异(
pct=n/长度(xdf),
pct_lab=sprintf(“有%s个点属于此级别”,比例::百分比(pct))
)
###tibble:12 x 4
##lvl n pct pct_实验室
##                                        
##1 0.002 270 0.993 99.3%的分数属于该水平
##2 0.004 259 0.952 95.2%的分数属于该水平
##3 0.006 249 0.915 91.5%的分数属于该水平
##4 0.008 232 0.853 85.3%的分数属于该水平
##5 0.01 206 0.757 75.7%的分数属于该水平
##6 0.012 175 0.643 64.3%的分数属于该水平
##7 0.01445 0.533 53.3%的分数属于该水平
##8 0.016 94 0.346 34.6%的分数属于该水平
##9 0.018 81 0.298 29.8%的分数属于该水平
##10 0.02 60 0.221 22.1%的分数属于该水平
##11 0.022 43 0.158 15.8%的分数属于该水平
##12 0.024 13 0.0478 4.8%的分数属于该水平
我只是为了避免喋喋不休,但百分比将根据您如何修改密度计算的各种参数而变化(我的
ggalt::geom_bkde2d()
使用了不同的估算器,也是如此)


如果有一种方法可以在不重新进行计算的情况下挑出百分比,那么最好的方法就是让其他人证明他们比写这个答案的人聪明多了(希望能用比最近看起来更圆滑的方式)。

谢谢。我将消化链接:)我看到你在那里发布,但我仍然不确定我是如何解释结果的,即每个级别意味着什么?我问,就像有人问我每个轮廓意味着什么,我无法解释。它是否简单到“如果有10个级别,则表示每个级别为10%”,因此标记为红色的数据是最密集的10%,下一个级别是最密集的20%。我无法用语言表达你的回答有多大帮助。这就是我想要的答案,还有更多!感谢您花时间回复。明天我一回到工作岗位,我就会运行额外的代码,这样我就能对他们做出回应,他们会非常高兴的!很高兴为您提供帮助,我可能会在ggplot2中使用PR,将其添加为一个额外的计算统计数据,可用于图例与级别,因为我也认为它更有用。嘿,代码运行了,但没有得到我想要的响应,不幸的是,它只显示了9个级别,而不是12个级别,其中5个级别没有数据。我注意到您在创建zdf之前创建了中断,所以我将其移动了。我是否需要更改某些内容以使其适合我的数据?我使用的是经度和纬度,我的“xdf”包含664个经度/纬度观测值。如果你想看一下,这是我的“xdf”数据:#ty!我很少在$DAYJOB中使用空间数据(我在网络安全部门工作),所以任何时候使用真实世界的空间数据都是一种享受!
SpatialPolygons(
  lapply(1:length(cl), function(idx) {
    Polygons(
      srl = list(Polygon(
        matrix(c(cl[[idx]]$x, cl[[idx]]$y), nrow=length(cl[[idx]]$x), byrow=FALSE)
      )),
      ID = idx
    )
  })
) -> cont

coordinates(xdf) <- ~x+y

data_frame(
  ct = sapply(over(cont, geometry(xdf), returnList = TRUE), length),
  id = 1:length(ct),
  lvl = sapply(cl, function(x) x$level)
) %>% 
  count(lvl, wt=ct) %>% 
  mutate(
    pct = n/length(xdf),
    pct_lab = sprintf("%s of the points fall within this level", scales::percent(pct))
  )
## # A tibble: 12 x 4
##      lvl     n    pct pct_lab                              
##    <dbl> <int>  <dbl> <chr>                                
##  1 0.002   270 0.993  99.3% of the points fall within this level
##  2 0.004   259 0.952  95.2% of the points fall within this level
##  3 0.006   249 0.915  91.5% of the points fall within this level
##  4 0.008   232 0.853  85.3% of the points fall within this level
##  5 0.01    206 0.757  75.7% of the points fall within this level
##  6 0.012   175 0.643  64.3% of the points fall within this level
##  7 0.014   145 0.533  53.3% of the points fall within this level
##  8 0.016    94 0.346  34.6% of the points fall within this level
##  9 0.018    81 0.298  29.8% of the points fall within this level
## 10 0.02     60 0.221  22.1% of the points fall within this level
## 11 0.022    43 0.158  15.8% of the points fall within this level
## 12 0.024    13 0.0478  4.8% of the points fall within this level