2017-11-03 6 views
1

맵 패키지의 맵을 래스터 데이터 (ggplot2 geom_tile의)에 오버레이하는 데 문제가 있습니까?R지도 패키지와 geom_tile을 오버레이하는 방법은 무엇입니까?

library(ggplot2) 
library(ggmap) 
library(maps) 
library(mapdata) 

여기 .. ​​내 데이터 여기

mydata = read.csv("Seasonal_Temp.csv") 
head(mydata) 
> head(mydata) 
longitude latitude seasonalTavg_Ens 
1 -111.688 40.500   3.435 
2 -111.688 40.563   3.183 
3 -111.688 40.625   3.488 
4 -111.625 40.500   3.437 
5 -111.625 40.563   3.395 
6 -111.625 40.625   3.429 

내가지도 내 래스터 데이터를 결합 할 수 아니에요지도

states <- map_data("state") 
dim(states) 
ut_df <- subset(states, region == "utah") 
head(ut_df) 

counties <- map_data("county") 
ut_county <- subset(counties, region == "utah") 
head(ut_county) 

area <- subset(ut_county, subregion %in% c("summit", "wasatch", "salt lake", 
"utah")) 

area_map <- ggplot(data = area, mapping = aes(x = long, y = lat, group = 
group)) + 
coord_fixed(1.3) + 
geom_polygon(color = "black", fill = "white") 
area_map + theme_bw() 

area_map + coord_fixed(xlim = c(-111.438, -111.688), ylim = c(40.5, 40.625), 
ratio = 1.3) 

의의

ggplot() + geom_raster(data = mydata, aes(longitude,latitude, fill = 
seasonalTavg_Ens)) 

감사 어떤 통찰력을 위해 당신! 나는 geom_raster 나 geom_tile을 사용하고 있습니다.

답변

0

당신이 ggplot을하고 그래서 객체에 저장 :

area_map <- ggplot(data = area, mapping = aes(x = long, y = lat, group = group)) + 
        coord_fixed(1.3) + 
        geom_polygon(color = "black", fill = "white") 

을 당신이 명시 적으로

area_map <- area_map + 
       theme_bw() + 
       coord_fixed(xlim = c(-111.438, -111.688), 
          ylim = c(40.5, 40.625), atio = 1.3) 
을 명시 할 필요가 같은 객체에 저장됩니다 더 많은 레이어를 추가 할

area_map + geom_raster(data = mydata, aes(longitude, latitude, 
         fill = seasonalTavg_Ens), inherit.aes = F, alpha = 0.5) 

또 다른 핵심 개념은,396,972,736를 지정하는 것입니다 :

는 geom_tile 레이어를 추가합니다 ggplot 호출에서 aes를 정의한 경우 특정 데이터를 사용하는 레이어로. 대부분의 경우 특정 데이터 레이어에 적용 할 수 실 거예요 이후 :

결과 :

enter image description here

전설을 일치 시키려면 하나는

area_map + geom_raster(data = mydata, 
         aes(longitude,latitude, 
          fill = seasonalTavg_Ens), 
         inherit.aes = F, 
         alpha = 0.5) + 
    guides(fill = guide_legend(override.aes = list(alpha = 0.5))) 

enter image description here

가 발생 할 수 전설은 계속 보이지 않는다. 이 문제를 해결하는 방법 대신 geom_polygon의 geom_path을 지정하고 모두 알파 소란을 방지하는 것입니다 :

ggplot() + 
    geom_raster(data = mydata, 
       aes(longitude,latitude, 
        fill = seasonalTavg_Ens)) + 
    geom_path(data = area, aes(x = long, y = lat, group = group), 
       color = "black")+ 
    coord_fixed(xlim = c(-111.438, -111.688), ylim = c(40.5, 40.625), 
       ratio = 1.3)+ 
    theme_bw() 

enter image description here

+0

감사합니다! 플롯과 전설의 색상이 일치하도록 범례를 음영 처리 할 수 ​​있습니까? – James

+0

@James 기꺼이 도와 드리겠습니다. 알파 do '+ 가이드 (fill = guide_legend (override.aes ​​= list (alpha = 0.5)))'와 일치시키기. 이것은 약간 다른 전설을 만들 것입니다. 답변을 업데이트합니다. – missuse