r - 如何提取等高线图的特定边界?

标签 r ggplot2 extract interpolation

考虑以下代码提供的计数图:

# input data x,y,z:
x<-rep(c(1:100),times=10)
y<-rep(c(1:10),each=100)
z<-NULL
for(i in 1:10){
  n<-sample(c(10:30),1)
  m<-sample(c(50:70),1)
  z<-c(z,c(1:n,sample(c(50:100),100-m-n),c(m:1)))}

# bilinear interpolation of irregular data:
library('akima')
interpo<-interp(x=x,y=y,z=z,xo=seq(min(x),max(x),length.out=1000),yo=seq(min(y),max(y),length.out=1000))
image(interpo)

# convert data format for ggplot:
x<-rep(interpo$x,times=1000)
y<-rep(interpo$y,each=1000)
r<-NULL
for(i in 1:1000){
  r<-c(r,interpo$z[,i])
}
plo<-data.frame(x,y,r)

# plot the countour plot:
library(ggplot2) 
library(scales)
ggplot(plo, aes(y = y, x = x,  fill = r)) +
  geom_raster()+
  scale_fill_gradient(low="blue",high="red",limits=c(min(r),max(r)))

剧情大概是这样的: enter image description here

现在我的问题是:

使用 R,是否可以提取和可视化 r 值大于 70 的某些边界(例如 r=70)?

最好能推导出边界的具体位置(z(x,y)的x、y坐标值不小于70)。

最佳答案

不太确定您的预期输出(如果您添加一些会很好),但以下可能接近:

# visualize the points where r = 70
ggplot(plo, aes(y = y, x = x,  fill = r)) +
  geom_raster()+
  scale_fill_gradient(low="blue",high="red",limits=c(min(r),max(r))) + 
  geom_point(data=plo[plo$r == 70,], col='white')      

enter image description here

# visualize the points where r > 70
ggplot(plo, aes(y = y, x = x,  fill = r)) +
  geom_raster()+
  scale_fill_gradient(low="blue",high="red",limits=c(min(r),max(r))) + 
  geom_point(data=plo[plo$r > 70,])

enter image description here

如果我们想(从 ggplot)取回 r > 70 的数据,我们可以尝试以下操作:

p <- ggplot(plo, aes(y = y, x = x,  fill = r)) +
  geom_raster()+
  scale_fill_gradient(low="blue",high="red",limits=c(min(r),max(r))) + 
  geom_point(data=plo[plo$r > 70,])

pg <- ggplot_build(p)
str(pg)
head(pg$data[[2]])
# fill        x y PANEL group shape colour size alpha stroke
#1 #E50056 26.96396 1     1    -1    19  black  1.5    NA    0.5
#2 #E70052 27.06306 1     1    -1    19  black  1.5    NA    0.5
#3 #E70050 27.16216 1     1    -1    19  black  1.5    NA    0.5
#4 #E8004F 27.26126 1     1    -1    19  black  1.5    NA    0.5
#5 #E9004D 27.36036 1     1    -1    19  black  1.5    NA    0.5
#6 #E9004C 27.45946 1     1    -1    19  black  1.5    NA    0.5

dplyr 的另一次尝试:

data <- data.frame(x=x, y=y, z=z)
dim(data)
#[1] 1000000       3
library(dplyr)
data <- plo %>% 
  inner_join(data, by=c('x'='x', 'y'='y')) %>% 
  filter(z >= 70 & r >= 70) # change the filter condition if needed
dim(data)
#[1] 10058     4
ggplot(plo, aes(y = y, x = x,  fill = r)) +
  geom_raster()+
  scale_fill_gradient(low="blue",high="red",limits=c(min(r),max(r))) + 
  geom_point(data=data) 

enter image description here

关于r - 如何提取等高线图的特定边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42464219/

相关文章:

c++ - C 的 R API 中 R.3.4.4 和 R.3.5.1 的区别

r - 使用 openrouteservice-r 为位置数据帧创建多个等时线

r - 如何使用 ggplot2 上的标识控制堆积条形图的排序

r - 向 autoplot.lm 添加通用标题

python - 是否有类似于运行 R-studio 服务器的类似 Python 服务器端 IDE(如 Spyder)?

r - 从 R 中数据框中的单元格中提取数字字符

javascript - 使用javascript从字符串中提取数字

Java-如何将正在运行的 jar 的内容提取到目录中?

反转ggplot中的geom_密度顺序

r - 使用scale_y_reverse()和stat_smooth()平滑geom_path()