2015-08-07 9 views
6

मैं ggplot2 में डेटा चर द्वारा मानचित्र प्लॉट को facet_wrap करना चाहता हूं - उदा। नीचे दिए गए उदाहरण में 'पालतू जानवर'। क्या प्रत्येक चर श्रेणी के लिए फोर्टिफाइड मानचित्र डेटा के पूर्ण डुप्लिकेशन की आवश्यकता है? वह मुझे थोड़ा मूर्खता के रूप में मार देगा। क्या कोई वैकल्पिक तरीका है?ggplot2 में facet_wrap के साथ मानचित्र

require(ggplot2) 
(nz_dat = data.frame(island = rep(c('North.Island ','South.Island '), 3), 
      pets = c('cats','cats','dogs','dogs','birds','birds'), 
      n = c(13, 26, 48, 74, 24, 17))) 
      island pets n 
1 North.Island cats 13 
2 South.Island cats 26 
3 North.Island dogs 48 
4 South.Island dogs 74 
5 North.Island birds 24 
6 South.Island birds 17 

nz = map_data("nz") 
nz = subset(nz, nz$region %in% c('North.Island ','South.Island ')) # 2 main islands 

# simple plot 
ggplot(nz, aes(long, lat, group=group, fill=factor(region))) + 
    geom_polygon() + coord_quickmap() 

enter image description here

+0

'coord_quickmap' ggplot में NZMG का अनुमान करने w/पूर्व के लिए सहारा ओ के लिए एक अच्छा विकल्प था पहले प्रक्षेपण! – hrbrmstr

उत्तर

13

आप सौंदर्यशास्त्र थोड़ा tweak कर सकते हैं:

library(ggthemes) # for theme_map 

gg <- ggplot() 

# lay down a base map (no borders or fills) 
# geom_map is a great way to do map layers like you would in any GIS 
gg <- gg + geom_map(data=nz, map=nz, 
        aes(x=long, y=lat, map_id=region), 
        color="#00000000", fill="#0000000", size=0.5) 

# since "island" equates to the "nz" map id of "region" use that 
# to "map" (in the data operation sense) the value in "n" to the 
# named polygon 
gg <- gg + geom_map(data=nz_dat, map=nz, 
        aes(fill=n, map_id=island, color=n)) 

# it's highly unlikely these values needed a continuous scale 
# so use a shortcut to colorbrewer with scale_*_distiller, scaling 
# both the color & fill so there are no black borders 
gg <- gg + scale_fill_distiller() 
gg <- gg + scale_color_distiller() 

# the OP did good here since it's better than Mercator for 
# NZ and NZMG coord system is not avail with coord_map() 
gg <- gg + coord_quickmap() 

# ggplot will make many maps! 
gg <- gg + facet_wrap(~pets) 

# ggplot will make clean maps! 
gg <- gg + theme_map() 

# put the legend wherever you want 
gg <- gg + theme(legend.position="right") 
gg 

enter image description here

+1

धन्यवाद कप्तान अमेरिका। मुझे इस विधि से अवगत नहीं था – geotheory

संबंधित मुद्दे