你如何将地图与ggplot2中复杂的点显示结合起来? [英] How do you combine a map with complex display of points in ggplot2?

查看:198
本文介绍了你如何将地图与ggplot2中复杂的点显示结合起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用研究地点绘制非洲背景地图。我可以独立创建两个,但我很难将它们叠加在彼此之上。



我使用的非洲地图是来自maplibrary的Esri shapefile。有机它可以从我的保管箱 https://www.dropbox.com/s/ etqdw3nky52czv4 /非洲%20map.zip 。我有一个文本文件中的点,也可以从我的下拉框中获得。 https://www.dropbox.com/s/scvymytjsr5pvaf/SPM-437 -22Nov12.txt 。他们提到有关疟疾寄生虫分子抗药性的研究。我想绘制它们,以便颜色是具有耐药遗传标记的寄生虫比例,大小是测试的寄生虫数量。

单独绘制点数:

  qplot(经度,纬度, data = d.spm.437,color = Frc437,size = Tot437)

绘制非洲地图:

  library(maptools)
africa = readShapePoly(Africa.shp)
africa.map = fortify(非洲,地区=国家)
qplot(long,lat,data = africa.map,geom =path,group = group)
pre>

任何帮助将这两者结合在一起,同时保留点的显示方式将不胜感激。

解决方案

尝试这样的事情。似乎为我工作。我认为你的一些经纬坐标是错误的。 geom_point 填充颜色目前设置为 Tot437 so你可能想改变它。



  library(ggplot2)
library(rgdal)

非洲< - readOGR( c:/ test,layer =Africa)
africa.map = fortify(非洲,region =COUNTRY)

africa.points = read.table(c:/ test / SPM-437-22Nov12.txt,header = TRUE,sep =,)
names(africa.points)[which(names(africa.points)=='Longitude')] < - 'long'#重命名lat并且与shp文件保持一致
名称(africa.points)[which(names(africa.points)=='Latitude')]< - 'lat'

ggplot(africa.map,aes(x = long,y = lat,group = group))+
geom_polygon(color =black,size = 1,fill =white,aes(group = group))+
geom_point(data = africa.points,aes(x = long,y = lat,fill = Tot437,group = NULL),size = 4,shape = 21,color =bla顺便说一句,看看你的地图,你可能很难得到一个好的详细信息查看个别区域,所以解决这个问题的一种方法是通过子集化(在这种情况下是数据框)。您可以这样做:

  africa.map<  -  africa.map [africa.map $ id =='Madagascar', ] 
africa.points< - africa.points [africa.points $ africa.points $ country =='Madagascar',]
ggplot(africa.map,aes(x = long,y = lat,group = group) ))+
geom_polygon(color =black,size = 1,fill =white,aes(group = group))+
geom_point(data = africa.points,aes(x = long ,y = lat,fill = Tot437,group = NULL),size = 2,shape = 21,color =black,size = 2)

...应该给你类似的东西:


I'm trying to plot points from study sites with a background map of Africa. I can create the two independently, but I am having a hard time overlaying them on top of eachother.

The map of Africa I am using is an Esri shapefile from maplibrary.org. It is available from my dropbox at https://www.dropbox.com/s/etqdw3nky52czv4/Africa%20map.zip. I have the points in a text file, also available from my drop box. https://www.dropbox.com/s/scvymytjsr5pvaf/SPM-437-22Nov12.txt. They refer to studies on molecular drug resistance of malaria parasites. I would like to plot them so that the color is the proportion of parasites with the drug resistant genetic marker and the size is the number of parasites tested.

Plotting the points independently:

qplot(Longitude, Latitude, data = d.spm.437, colour = Frc437, size = Tot437)

Plotting the map of Africa:

library(maptools)
africa = readShapePoly("Africa.shp")
africa.map = fortify(africa, region="COUNTRY")
qplot(long, lat, data = africa.map, geom="path", group=group)

Any help on putting these two together while preserving the display of the points would be appreciated.

解决方案

Try something like this. Seems to work for me. I think some of your lat-long coordinates are wrong though. The fill colour for geom_point is currently set to Tot437 so you might want to change that.

library(ggplot2)
library(rgdal)

africa <- readOGR("c:/test", layer = "Africa")
africa.map = fortify(africa, region="COUNTRY")

africa.points = read.table("c:/test/SPM-437-22Nov12.txt", header = TRUE, sep = ",")
names(africa.points)[which(names(africa.points) == 'Longitude')] <- 'long' # rename lat and long for consistency with shp file
names(africa.points)[which(names(africa.points) == 'Latitude')] <- 'lat'

ggplot(africa.map, aes(x = long, y = lat, group = group)) +
    geom_polygon(colour = "black", size = 1, fill = "white", aes(group = group)) +
    geom_point(data = africa.points, aes(x = long, y = lat, fill = Tot437, group = NULL), size = 4, shape = 21, colour = "black", size = 3)

Incidentally, looking at your map you may have difficulty getting a good detailed view of individual areas, so one way to tackle that would be by subsetting, in this case with the data frames. You could do this:

africa.map <- africa.map[africa.map$id == 'Madagascar', ]
africa.points <- africa.points[africa.points$Country == 'Madagascar', ]
ggplot(africa.map, aes(x = long, y = lat, group = group)) +
    geom_polygon(colour = "black", size = 1, fill = "white", aes(group = group)) +
    geom_point(data = africa.points, aes(x = long, y = lat, fill = Tot437, group = NULL), size = 2, shape = 21, colour = "black", size = 2)

...which should get you something similar to this:

这篇关于你如何将地图与ggplot2中复杂的点显示结合起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆