2013-04-16 98 views
17

我想用GGPLOT2, 產生了地圖上的每個位置的barplot作爲被xingmowang與基地圖形做,許多包:使用ggplot2在地圖上繪製條形圖?

http://nzprimarysectortrade.wordpress.com/2011/10/02/let-r-fly-visualizing-export-data-using-r/

enter image description here

這是有關到Embedding a miniature plot within a plot

就目前而言,我能做的最好的是匹配值在抖動的點積點大小:

require(ggplot2) 
require(maps) 

#Get world map info 
world_map <- map_data("world") 

#Creat a base plot 
p <- ggplot() + coord_fixed() 

#Add map to base plot 
base_world <- p + geom_polygon(data=world_map, 
           aes(x=long, 
            y=lat, 
            group=group)) 


#Create example data 
geo_data <- data.frame(long=c(20,20,100,100,20,20,100,100), 
         lat=c(0,0,0,0,0,0,0,0), 
         value=c(10,30,40,50,20,20,100,100), 
         Facet=rep(c("Facet_1", "Facet_2"), 4), 
         colour=rep(c("colour_1", "colour_2"), each=4)) 

#Creat an example plot 
map_with_jitter <- base_world+geom_point(data=geo_data, 
              aes(x=long, 
               y=lat, 
               colour=colour, 
               size=value), 
             position="jitter", 
             alpha=I(0.5)) 

#Add faceting 
map_with_jitter <- map_with_jitter + facet_wrap(~Facet) 

map_with_jitter <- map_with_jitter + theme(legend.position="none") 

print(map_with_jitter) 

Example map plot with ggplot2

隨着不雅的解決辦法:

subset_data <- geo_data[geo_data$Facet=="Facet_1" & 
          geo_data$long=="20",] 
subplot <- qplot(data=subset_data, 
       x=colour, 
       y=value, 
       fill=colour, 
       geom="bar", 
       stat="identity")+theme(legend.position="none") 

print(base_world) 
print(subplot, vp=viewport((200+mean(subset_data$long))/400,(100+mean(subset_data$lat))/200 , .2, .2)) 

enter image description here

+1

相關問題:http://stackoverflow.com/questions/10368180/plotting-pie-graphs-on-map-in-ggplot –

+2

有什麼問題嗎?你有什麼嘗試? –

+0

請參閱'?annotation_custom'獲取嵌入圖形示例 – baptiste

回答

23

向上日期2016年12月23日:該ggsubplot -package不再是積極維護,是archived on CRAN

包「ggsubplot」從CRAN庫中刪除>
以前可用版本可以從檔案中獲得。>
按照維護者[email protected]的要求存檔於2016-01-11。

ggsubplot將不適用於R版本> = 3.1.0。安裝R 3.0.3以下運行代碼:


可以確實是由ggsubplot包像巴蒂斯特指出的手段實現這一目標。

library(ggsubplot) 
library(ggplot2) 
library(maps) 
library(plyr) 

#Get world map info 
world_map <- map_data("world") 

#Create a base plot 
p <- ggplot() + geom_polygon(data=world_map,aes(x=long, y=lat,group=group)) 

# Calculate the mean longitude and latitude per region, these will be the coördinates where the plots will be placed, so you can tweak them where needed. 
# Create simulation data of the age distribution per region and merge the two. 

centres <- ddply(world_map,.(region),summarize,long=mean(long),lat=mean(lat)) 
mycat <- cut(runif(1000), c(0, 0.1, 0.3, 0.6, 1), labels=FALSE) 
mycat <- as.factor(mycat) 
age <- factor(mycat,labels=c("<15","15-30","20-60",">60")) 
simdat <- merge(centres ,age) 
colnames(simdat) <- c("region","long","lat","Age") 

# Select the countries where you want a subplot for and plot 
simdat2 <- subset(simdat, region %in% c("USA","China","USSR","Brazil", "Australia")) 
(testplot <- p+geom_subplot2d(aes(long, lat, subplot = geom_bar(aes(Age, ..count.., fill = Age))), bins = c(15,12), ref = NULL, width = rel(0.8), data = simdat2)) 

結果:enter image description here

+0

任何方式添加ay軸或至少一個參考欄,顯示y軸上給定數量的單位的高度(ref:ref_box,ref_hline,ref_vline只允許你指定相對於繪圖區而不是y軸單位)?我想可以將一個已知的數據點添加到每個bin中,並在y軸上有一個已知值,然後將顯示在圖例中。其他解決方案? –

+0

我從來沒有這樣做過,所以我不知道。我會檢查。 – JT85

+0

有人可能會澄清'geom_bar'的'..count..'參數是如何工作的嗎? – geotheory