如何提取一个countour图的某个边界?

时间:2017-02-26 02:55:49

标签: r ggplot2 extract interpolation

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

def generate_em(minimum, maximum, list)
  digits_min = minimum.to_s.size
  digits_min += 1 if minimum > (list.max.to_s*digits_min).to_i
  digits_max = maximum.to_s.size
  digits_max -= 1 if maximum < (list.min.to_s*digits_max).to_i
  (digits_min..digits_max).each_with_object([]) { |n,arr|
    arr.concat(list.repeated_permutation(n).to_a.map { |a| a.join.to_i }) }.
      uniq.
      select { |n| (minimum..maximum).cover?(n) }
end

情节是这样的: enter image description here

现在我的问题是:

使用R,是否可以提取并可视化minimum = 1 maximum = 100 list = [1, 2, 3] generate_em(minimum, maximum, list) #=> [1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33] 值大于70的某些边界(例如r = 70)?

最好是可以导出边界的特定位置(z(x,y)的x和y坐标值不小于70)。

2 个答案:

答案 0 :(得分:1)

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

# 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

如果我们想要获取数据(来自ggplotr > 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

答案 1 :(得分:1)

您还可以使用data.table来帮助解决问题的视觉方面。我使用 library(data.table) plo2 <- data.table(plo) more <- plo2[r>=70] more2 <- plo2[r==70] ggplot() + geom_raster(data = plo, aes(y = y, x = x, fill = r))+ scale_fill_gradient(low="blue",high="red",limits=c(min(r),max(r)))+ geom_density_2d(aes(more$x,more$y), color = "black", size = .68)+ geom_point(aes(more2$x,more2$y), color = "white") 来准备数据如下:

cmd.exe /start.... 

enter image description here