2017-09-04 9 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

당신은 수를 또한 텍스트 크기를 사용하여 영역을 강조 표시하십시오. 가장 높은 밀도의 :

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