使用ggplot2在地图上绘制条形图?

Eti*_*rie 17 maps plot r bar-chart ggplot2

我想使用ggplot2为地图上的每个位置生成一个条形图,就像xingmowang使用基本图形和许多包一样:

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

在此输入图像描述

这与在图中嵌入微型图有关.

目前,我能做的最好的事情是在抖动的点图中将值与点大小匹配:

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)
Run Code Online (Sandbox Code Playgroud)

使用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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

JT8*_*T85 23

更新2016-12-23:ggsubplot不再主动维护-package并将其存档在CRAN上:

包'ggsubplot'已从CRAN存储库中删除.>
以前可用的版本可以从存档中获得.>
按照维护者garrett@rstudio.com的要求在2016-01-11存档.

ggsubplot不适用于R版本> = 3.1.0.安装R 3.0.3以运行以下代码:


你可以通过ggsubplotBaptiste建议的方式实现这一点.

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))
Run Code Online (Sandbox Code Playgroud)

结果:在此输入图像描述

  • 前几天,我无法用ggsubplot复制其中一个例子.今天,我尝试了这个例子,但我无法复制它.我有一条错误消息说"layout_base中的错误(数据,变量,drop = drop):至少有一层必须包含用于facetting的所有变量".我想知道你是否有相同的错误信息.你能测试一下你的剧本吗? (3认同)
  • 或者:`simdat3 = plyr :: count(simdat2,vars = c('region','long','lat','Age'))`并用`p + geom_subplot绘图(data = simdat3,aes(long, lat,group = region,subplot = geom_bar(aes(年龄,频率,填充=年龄),stat ="identity")),width = 10,height = 20) (2认同)