2017-09-04 7 views
1

私は紛失した船の動きをシミュレートするプロジェクトを進めています。これは、このようなマップを作成するグラフの一点あたりのカウント数を表示する(統計)

library("ggplot2") 

a <- rnorm(1000, 30.2, 2) 
b <- rnorm(1000, 10, 5) 
y <- (x + a + b) * 0.6 

df <- data.frame(x,y) 
p <- ggplot(df,aes(x=x,y=y)) + 
    ggtitle("A Priori Map") + xlab("Longtitude") + ylab("Latitude") + 
    scale_fill_gradientn(colors = topo.colors(10)) 

p + stat_binhex(show.legend = T, bins = 20) 

:私はこのコードを使用して分布マップを作ったしかし

A hexbin map

を、代わりの色を使用してカウント数を示す、私は希望しますあるポイントに実際のカウントを表示します。したがって、プログラムが特定のポイントで3回「着陸」した場合、「3」と表示されます。

これはどのようにRで実行できますか?

+0

あなたのサンプルデータには 'X'はありません。 – eipi10

答えて

5

ここでは、既存のグラフにカウントを追加する方法は次のとおりです。あなたが行うことができ、塗りつぶしの色を削除するには

library(ggplot2) 
theme_set(theme_bw()) 

set.seed(2) 
a <- rnorm(1000, 30.2, 2) 
b <- rnorm(1000, 10, 5) 
x = rnorm(1000) 
y <- (x + a + b) * 0.6 

df <- data.frame(x,y) 

p <- ggplot(df,aes(x=x,y=y)) + 
    ggtitle("A Priori Map") + 
    xlab("Longtitude") + ylab("Latitude") + 
    scale_fill_gradientn(colors = topo.colors(10)) + 
    stat_binhex(show.legend = T, bins = 20) 

p + geom_text(stat="binhex", bins=20, aes(label=..count..), show.legend=FALSE, 
       colour=hcl(15,100,60), fontface="bold", size=3.5) 

enter image description here

ggplot(df,aes(x=x,y=y)) + 
    ggtitle("A Priori Map") + 
    xlab("Longtitude") + ylab("Latitude") + 
    stat_binhex(bins = 20, fill=NA, colour="black") + 
    geom_text(stat="binhex", bins=20, aes(label=..count..), colour="red") 

enter image description here

あなたは可能性がありまた、テキストサイズを使用して領域を強調表示する最高密度のS:

ggplot(df,aes(x=x,y=y)) + 
    ggtitle("A Priori Map") + 
    xlab("Longtitude") + ylab("Latitude") + 
    stat_binhex(show.legend = T, bins = 20, fill=NA, colour="grey70") + 
    geom_text(stat="binhex", bins=20, aes(label=..count.., size=..count..), colour="red") + 
    scale_size_continuous(range=c(3,6)) + 
    guides(size=FALSE) 

enter image description here

また、六角グリッドなしで動作します:

ggplot(df,aes(x=x,y=y)) + 
    ggtitle("A Priori Map") + 
    xlab("Longtitude") + ylab("Latitude") + 
    geom_text(stat="binhex", bins=20, aes(label=..count.., size=..count..), colour="red") + 
    scale_size_continuous(range=c(3,6)) + 
    guides(size=FALSE) 

enter image description here

+0

まさに私が必要としていたものです。お手伝いありがとう! – RedGreenBlue

+0

これが必要な場合は、「回答を受け入れる」チェックボックスをクリックしてください。 – eipi10

関連する問題