2013-04-16 66 views
17

Tôi muốn tạo ra một barplot cho mỗi vị trí trên bản đồ bằng ggplot2, như đã được thực hiện bằng cách xingmowang với đồ họa cơ bản và một số gói:Vẽ sơ đồ thanh trên bản đồ bằng ggplot2?

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

enter image description here

Đây là liên quan đến Embedding a miniature plot within a plot.

Đối với thời điểm này, tốt nhất mà tôi có thể làm là giá trị phù hợp để chỉ kích thước trong một âm mưu điểm jittered:

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

Với một workaround thanh nha:

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

câu hỏi liên quan: http://stackoverflow.com/questions/10368180/plotting-pie-graphs-on-map-in-ggplot –

+2

câu hỏi là gì ở đây? Bạn đã thử cái gì? –

+0

xem '? Annotation_custom' để biết ví dụ về hình ảnh inset – baptiste

Trả lời

23

Up ngày 2016/12/23: Các ggsubplot -package không còn tích cực duy trì và là archived on CRAN:.

Package 'ggsubplot' đã bị xóa khỏi kho cran>
các phiên bản trước đây có sẵn có thể được lấy từ các kho lưu trữ .>
Đã lưu trữ vào ngày 2016-01-11 theo yêu cầu của người bảo trì [email protected]

ggsubplot sẽ không hoạt động với phiên bản R> = 3.1.0. Cài đặt R 3.0.3 để chạy các đoạn mã sau:


Bạn thực sự có thể đạt được điều này bằng phương tiện của gói ggsubplot như Baptiste gợi ý.

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)) 

Kết quả: enter image description here

+0

Bất kỳ cách nào để thêm trục ay hoặc ít nhất một thanh tham chiếu hiển thị hight của một số đơn vị nhất định trên trục y (ref: ref_box, ref_hline, ref_vline chỉ cho phép bạn chỉ định tương đối với khu vực lô không phải đơn vị trục y)? Tôi đoán người ta có thể thêm một điểm dữ liệu đã biết cho mỗi thùng với một giá trị đã biết trên trục y mà sau đó sẽ được trình bày trong chú giải. Bất kỳ giải pháp nào khác? –

+0

Tôi chưa bao giờ làm điều đó, vì vậy tôi không biết. Tôi sẽ kiểm tra. – JT85

+0

Có thể ai đó có thể làm rõ cách đối số 'geom_bar'' 'count..' hoạt động như thế nào? – geotheory

Các vấn đề liên quan