2014-07-06 2 views
2

위도, 경도 및 개수 형식의 고객 데이터가 일부 제공되었습니다. ggplot 히트 맵을 생성하는 데 필요한 모든 데이터가 있지만 ggplot이 요구하는 형식으로 데이터를 저장하는 방법을 모르겠습니다.ggplot 사용을위한 위도, 경도 및 개수 요약 데이터

데이터를 0.01 Lat 및 0.01 Lon 블록 (일반적인 히트 맵) 내에서 총계하여 집계하려고하며 본능적으로 "tapply"라고 생각했습니다. 원하는대로 블록 크기별로 멋진 요약을 만들지 만 형식이 잘못되었습니다. 더구나, 나는 거기에 아무것도 없더라도, 빈 Lat 또는 Lon 블록 값을 0으로 포함 시키길 정말로 원합니다. 그렇지 않으면 히트 맵이 이상하고 이상하게 보입니다.

귀하의 도움에 감사드립니다.

나는 아래의 코드에서 참조 용으로 내 데이터의 하위 집합을 만들었습니다

여기
# m is the matrix of data provided 
m = matrix(c(44.9591051,44.984884,44.984884,44.9811399, 
      44.9969096,44.990894,44.9797023,44.983334, 
      -93.3120017,-93.297668,-93.297668,-93.2993524, 
      -93.2924484,-93.282462,-93.2738911,-93.26667, 
      69,147,137,22,68,198,35,138), nrow=8, ncol=3) 
colnames(m) <- c("Lat", "Lon", "Count") 
m <- as.data.frame(m) 
s = as.data.frame((tapply(m$Count, list(round(m$Lon,2), round(m$Lat,2)), sum))) 
s[is.na(s)] <- 0 

# Data frame "s" has all the data, but not exactly in the format desired... 
# First, it has a column for each latitude, instead of one column for Lon 
# and one for Lat, and second, it needs to have 0 as the entry data for 
# Lat/Lon pairs that have no other data. As it is, there are only zeroes 
# when one of the other entries has a Lat or Lon that matches... if there 
# are no entries for a particular Lat or Lon value, then nothing at all is 
# reported. 

desired.format = matrix(c(44.96,44.96,44.96,44.96,44.96, 
    44.97,44.97,44.97,44.97,44.97,44.98,44.98,44.98, 
    44.98,44.98,44.99,44.99,44.99,44.99,44.99,45,45, 
    45,45,45,-93.31,-93.3,-93.29,-93.28,-93.27,-93.31, 
    -93.3,-93.29,-93.28,-93.27,-93.31,-93.3,-93.29, 
    -93.28,-93.27,-93.31,-93.3,-93.29,-93.28,-93.27, 
    -93.31,-93.3,-93.29,-93.28,-93.27,69,0,0,0,0,0,0, 
    0,0,0,0,306,0,0,173,0,0,0,198,0,0,0,68,0,0), 
    nrow=25, ncol=3) 

colnames(desired.format) <- c("Lat", "Lon", "Count") 
desired.format <- as.data.frame(desired.format) 

minneapolis = get_map(location = "minneapolis, mn", zoom = 12) 
ggmap(minneapolis) + geom_tile(data = desired.format, aes(x = Lon, y = Lat, alpha = Count), fill="red") 
+0

geom_hex 및 stat_density2d와 같은 "기본"솔루션을 살펴 보았습니까? – ako

+0

아코 ... 아니야. 나는이 특별한 종류의 시각화에 비교적 익숙하지 않으며 조사를위한 새로운 방향을 고맙게 생각합니다. 나는이 두 가지를 살펴볼 것이다. 고맙습니다. – rucker

+0

아코 ... 그냥 geom_hex를 살펴 봤는데 같은 형식을 찾고있는 것 같습니다. 근본적인 문제는 ggplot이 행당 개수 값이 아니라 각 행에 대해 개별 항목을 필요로한다는 것입니다. 상황을 악화시키는 것은 이러한 카운트 값을 히트 맵 (또는 헥스 맵)에 원하는 공간적 위치로 집계해야한다는 것입니다. – rucker

답변

3

는 geom_hex와 stat_density2d와 자상이다. 좌표를 잘라서 쓰레기통을 만드는 아이디어는 조금 불안합니다.

위도/경도가 주어지는 계산 데이터입니다. 이는 가중치 매개 변수가 필요하다는 것을 의미하지만 이상적으로 geom_hex로 구현되지 않았다는 것을 의미합니다. 대신, 우리는 접근 변수 here과 비슷한 count 변수 당 행을 반복하여 해킹합니다.

## hack job to repeat records to full count 
    m<-as.data.frame(m) 
    m_long <- with(m, m[rep(1:nrow(m), Count),]) 


    ## stat_density2d 
    ggplot(m_long, aes(Lat, Lon)) + 
    stat_density2d(aes(alpha=..level.., fill=..level..), size=2, 
       bins=10, geom=c("polygon","contour")) + 
    scale_fill_gradient(low = "blue", high = "red") + 
    geom_density2d(colour="black", bins=10) + 
    geom_point(data = m_long) 


    ## geom_hex alternative 
    bins=6 
    ggplot(m_long, aes(Lat, Lon)) + 
    geom_hex(bins=bins)+ 
    coord_equal(ratio = 1/1)+ 
    scale_fill_gradient(low = "blue", high = "red") + 
    geom_point(data = m_long,position = "jitter")+ 
    stat_binhex(aes(label=..count..,size=..count..*.5), size=3.5,geom="text", bins=bins, colour="white") 

이 각각 다음과 같은 생산 : enter image description here 그리고 비닝 버전 : enter image description here

편집 :베이스 맵으로

:

map + 
    stat_density2d(data = m_long, aes(x = Lon, y = Lat, 
alpha=..level.., fill=..level..), 
       size=2, 
       bins=10, 
       geom=c("polygon","contour"), 
       inherit.aes=FALSE) + 
    scale_fill_gradient(low = "blue", high = "red") + 
    geom_density2d(data = m_long, aes(x = Lon, y=Lat), 
       colour="black", bins=10,inherit.aes=FALSE) + 
    geom_point(data = m_long, aes(x = Lon, y=Lat),inherit.aes=FALSE) 


## and the hexbin map... 

map + #ggplot(m_long, aes(Lat, Lon)) + 
    geom_hex(bins=bins,data = m_long, aes(x = Lon, y = Lat),alpha=.5, 
       inherit.aes=FALSE) + 
    geom_point(data = m_long, aes(x = Lon, y=Lat), 
      inherit.aes=FALSE,position = "jitter")+ 
    scale_fill_gradient(low = "blue", high = "red") 

enter image description here ,663,

+0

아코, 고마워요! 그것들은 예외적으로 차갑고, 내가하려고했던 것보다 훨씬 낫습니다! 더 좋은 점은 카운트의 추가를 기본적으로 훌륭하게 처리한다는 것입니다. 그러나, 제 신청서에는 해석을 안내하기 위해 아래에지도가 필요합니다. geom_hex 코드를 기존 ggmap 코드에 "넣으려고"했으나 폭탄이 터졌습니다. 이 둘 중 하나의 아래에지도를 놓고 그래프를 볼 수 있도록 알파 (투명도)를 변경 할 수 있습니까? – rucker

+0

수정 사항을 확인하십시오. – ako

+0

투표를 수락하고 닫을 수 있습니까? – ako