28

平面上に一連の点があります。それらはサブセットに分割されます。同じサブセットに属する点の周りに閉曲線をプロットして、サブセットに属する点が曲線の内側になり、そうでない点が外側になるようにします。したがって、単純な円や凸包は機能しない可能性があります。

手始めに、一連の点の周りに滑らかな曲線を描きたいだけだとしましょう (他の点を除外する必要はありません)。

Rでそれを行う方法はありますか?

---後から追記---

私が最終的に見ているのは、ここのグラフィックスの精神にあるものです: https://tex.stackexchange.com/questions/1175/drawing-a-hypergraph - コンテキストはハイパーグラフではなく、与えられたポイントのセットとそれらのパーティション。

4

4 に答える 4

22

さて、あなたが追いかけているものに近いと思う回答のバージョンを次に示します。これは、GIS フォーラムのspline.polyこの回答 ( https://gis.stackexchange.com/a/24929 ) で作成された関数を使用します。

以下にいくつかの例を示します。

testpts <- 
structure(list(x = c(4.9, 4.2, 4, 4.1, 4.4, 5.8, 5.8, 5.8, 5.8, 
5.5, 4.9, 3.2, 3.2, 3.3, 5.4, 5.4, 5.7, 6.4, 6.7, 6.7, 6, 4.8, 
3.6, 2.8, 3.5, 4.4, 5.1, 4, 3.7, 4.5, 4.9, 5.7), y = c(6.9, 6.2, 
5.3, 4.1, 3.1, 2.9, 2.9, 3.5, 4.2, 4.9, 5.1, 4.9, 4.9, 5.2, 6.9, 
6.9, 5.3, 3.8, 4.2, 5.6, 6.9, 5.8, 1.2, 2.5, 5.3, 6.4, 6.8, 7.6, 
6.9, 5.4, 4.8, 4.4)), .Names = c("x", "y"))

基本的なプロットを設定する

plot(NA,xlim=c(0,10),ylim=c(0,10))
points(testpts,pch=19)
chuld <- lapply(testpts,"[",chull(testpts))
polygon(chuld,lty=2,border="gray")
polygon(spline.poly(as.matrix(as.data.frame(chuld)),100),border="red",lwd=2)

そして結果:

チュリン・イット・アップ!

凹例を追加するための編集

回答のこの部分では、alphahullライブラリを使用しています

# load the required library
library(alphahull)

plot(NA,xlim=c(0,10),ylim=c(0,10))
points(testpts,pch=19)
# remove duplicate points so the ahull function doesn't error out
testptsnodup <- lapply(testpts,"[",which(!duplicated(as.matrix(as.data.frame(testpts)))))

ahull オブジェクトを生成してプロットします。ポリゴンがデータに適合するかどうかを判断するには、アルファ値が非常に重要であると思われます。

ahull.obj <- ahull(testptsnodup,alpha=2)
plot(ahull.obj,add=TRUE,col="red",wpoints=FALSE)

そして結果:

ここに画像の説明を入力

于 2012-11-27T08:20:40.083 に答える
9

ggaltパッケージは、次のgeom_encircleようなものを提供することになっている を提供します-凸状ですが、滑らかです:

library(ggplot2)
library(ggalt)  ## v 0.4.0

df <- data.frame(x = rnorm(20), y = rnorm(20),
      z = sample(letters[1:5], 20, replace = TRUE))
ggplot(df, aes(x, y, colour = z)) + geom_point() +
     geom_encircle(aes(fill=z),alpha=0.3)

ここに画像の説明を入力

于 2016-12-09T03:36:02.650 に答える
3

グーグルで調べた後、この例を少し変更しますMorota ggplot2

編集

ベジエでchull関数を使用

library(ggplot2)
library(plyr)
library(Hmisc)



df <- data.frame(x = rnorm(20), y = rnorm(20),z = sample(letters[1:5], 20, rep = T))
ggplot(df, aes(x, y, colour = z)) + geom_point()

find_hull <- function(df) {
    res.ch <- df[chull(df$x, df$y), ]
    res <- bezier(res.ch)
    res <- data.frame(x=res$x,y=res$y)
    res$z <- res$z
    res
  }
hulls <- ddply(df, "z", find_hull)
ggplot(df, aes(x, y, colour = z,fill = z)) +
  geom_point() + geom_polygon(data = hulls,alpha = 0.4)

ここに画像の説明を入力

于 2012-11-27T06:36:18.553 に答える
0

単に:

testpts <- structure(list(x = c(4.9, 4.2, 4, 4.1, 4.4, 5.8, 5.8, 5.8, 5.8, 
5.5, 4.9, 3.2, 3.2, 3.3, 5.4, 5.4, 5.7, 6.4, 6.7, 6.7, 6, 4.8, 
3.6, 2.8, 3.5, 4.4, 5.1, 4, 3.7, 4.5, 4.9, 5.7), y = c(6.9, 6.2, 
5.3, 4.1, 3.1, 2.9, 2.9, 3.5, 4.2, 4.9, 5.1, 4.9, 4.9, 5.2, 6.9, 
6.9, 5.3, 3.8, 4.2, 5.6, 6.9, 5.8, 1.2, 2.5, 5.3, 6.4, 6.8, 7.6, 
6.9, 5.4, 4.8, 4.4)), .Names = c("x", "y"))
x <- do.call('cbind',testpts)
ch<-chull(x)
x[c(ch,ch[1]),]
plot(x,pch=20)
points(x[ch,],pch=20,col='red')
lines(x[c(ch,ch[1]),],lwd=.5)

プロット:

プロット

于 2014-01-23T21:53:51.410 に答える