在R地图上绘制csv文件中的数据,突出显示它们的不同来源

时间:2015-10-14 15:32:40

标签: r plot maps rworldmap

我是新手使用R而我发现很难绘制这些数据 我已经获得了我的地图(使用rworldmap)。这里是我使用的2行代码:

library(rworldmap)
newmap <- getMap(resolution = "low")
plot(newmap)
plot(newmap, xlim = c(-180, 180), ylim = c(-90, 90), asp = 1, fill=TRUE, col="white", bg="lightblue",)

然后我按照以下步骤显示我的.csv文件的内容

coordinates <- read.csv("seqmap.csv", header=T, fill=T, col.names=c("ID CODE", "latitude", "longitude", "sample nature", "seq.methods"), row.names=NULL)
head(coordinates)

现在出现问题: 我想要多边形:

样品性质的SQUARE =沉积物

CIRCLE用于样本性质=水

作为所选多边形的填充颜色

1)红色用于seq.methods = Illumina

2)seq.methods = 454

的蓝色

3)绿色为seq.methods =离子洪流

4)YELLOW for seq.methods = Sanger

作为例子: 对于用Illumina测序的沉积物样品:红色作为填充颜色的正方形

对于用454测序的水样本:蓝色为填充色的圆圈

我被困在这一点上,据我所知,我觉得我无法再进一步:( 在以下链接中,您可以找到.csv文件 https://www.dropbox.com/s/t812lf6xgc0d6kf/seqmap.csv?dl=0

提前感谢您的任何帮助,对不起任何语法/拼写错误,英语不是我的第一语言:)

2 个答案:

答案 0 :(得分:0)

如果您可以使用字符而不是多边形,您可能需要尝试这样:

library(rworldmap)
newmap <- getMap(resolution = "low")
download.file("https://www.dropbox.com/s/t812lf6xgc0d6kf/seqmap.csv?dl=1", csv <- tempfile(fileext = ".csv"))
coordinates <- read.csv(csv, header=T, fill=T, sep=";", col.names=c("ID CODE", "latitude", "longitude", "sample nature", "seq.methods"), row.names=NULL)

pdf(pdffile <- tempfile(fileext = ".pdf"), width = 80, height = 40)
plot(newmap, xlim = c(-180, 180), ylim = c(-90, 90), asp = 1, fill=TRUE, col="white", bg="lightblue",)

with(coordinates, 
     points(x=longitude, y=latitude, cex=.5, # reduce symbol size a bit
            col=adjustcolor(c("Illumina"="red", "454"="blue", "ion torrent"="green", "Sanger"="yellow")[as.character(seq.methods)], alpha.f = .5), 
            pch=c("sediment"=15, "water"=19)[as.character(sample.nature)]
     )
)
dev.off()
shell.exec(pdffile) # open pdf doc on windows to zoom/pan easily...

答案 1 :(得分:0)

由于您的数据点遍布全球,因此在一个地图中绘制所有内容时,很难区分地图上的点。作为替代方案,您可以创建一个带有特写的切面图,用于世界上的几个地方。我建议采取以下步骤:

1:阅读数据并确保他们拥有合适的班级:

coordinates <- read.csv2("seqmap.csv", header=T, fill=T,
                         col.names=c("ID.CODE", "latitude", "longitude", "sample.nature", "seq.methods"),
                         row.names=NULL)
coordinates$latitude <- as.numeric(as.character(coordinates$latitude))
coordinates$longitude <- as.numeric(as.character(coordinates$longitude))

2:加载所需的库:

library(ggplot2)
library(ggmap)
library(grid)
library(gridExtra)

3:创建点群集并为点分配群集值:

km <- kmeans(coordinates[,c("longitude","latitude")], centers = 6)
coordinates$cluster <- km$cluster

4:获取不同群集的地图:

> km$centers
  longitude  latitude
1 -156.7000  71.37000
2  -88.3875  28.82875
3  -39.0700 -17.65200
4  139.3400  35.88667
5   94.4150 -67.29500
6  -64.5000  32.17000

map1 <- get_map(location = c(lon = -156.7, lat = 71.37), zoom = 7)
map2 <- get_map(location = c(lon = -88.3875, lat = 28.82875), zoom = 9)
map3 <- get_map(location = c(lon = -39.07, lat = -17.652), zoom = 9)
map4 <- get_map(location = c(lon = 139.34, lat = 35.88667), zoom = 8)
map5 <- get_map(location = c(lon = 94.415, lat = -67.295), zoom = 4)
map6 <- get_map(location = c(lon = -64.5, lat = 32.17), zoom = 10)

5:创建地图并将其存储为对象:

p1 <- ggmap(map1) +
  geom_point(data=coordinates[coordinates$cluster==1,], aes(x=longitude, y=latitude, shape=sample.nature, fill=seq.methods), size=3) +
  scale_shape_manual(values=c("sediment"=21,"water"=22)) +
  scale_fill_manual(values=c("Illumina"="red")) +
  guides(shape=FALSE, fill=FALSE) +
  ggtitle("Alaska")

p2 <- ggmap(map2) +
  geom_point(data=coordinates[coordinates$cluster==2,], aes(x=longitude, y=latitude, shape=sample.nature, fill=seq.methods), size=3) +
  scale_shape_manual(values=c("sediment"=21,"water"=22)) +
  scale_fill_manual(values=c("Illumina"="red","ion torrent"="green")) +
  guides(shape=FALSE, fill=FALSE) +
  ggtitle("New Orleans")

p3 <- ggmap(map3) +
  geom_point(data=coordinates[coordinates$cluster==3,], aes(x=longitude, y=latitude, shape=sample.nature, fill=seq.methods), size=3) +
  scale_shape_manual(values=c("water"=22)) +
  scale_fill_manual(values=c("454"="blue")) +
  guides(shape=FALSE, fill=FALSE) +
  ggtitle("Brazil")

p4 <- ggmap(map4) +
  geom_point(data=coordinates[coordinates$cluster==4,], aes(x=longitude, y=latitude, shape=sample.nature, fill=seq.methods), size=3) +
  scale_shape_manual(values=c("sediment"=21)) +
  scale_fill_manual(values=c("454"="blue")) +
  guides(shape=FALSE, fill=FALSE) +
  ggtitle("Japan")

p5 <- ggmap(map5) +
  geom_point(data=coordinates[coordinates$cluster==5,], aes(x=longitude, y=latitude, shape=sample.nature, fill=seq.methods), size=3) +
  scale_shape_manual(values=c("water"=22)) +
  scale_fill_manual(values=c("Sanger"="yellow")) +
  guides(shape=FALSE, fill=FALSE) +
  ggtitle("Antartica")

p6 <- ggmap(map6) +
  geom_point(data=coordinates[coordinates$cluster==6,], aes(x=longitude, y=latitude, shape=sample.nature, fill=seq.methods), size=3) +
  scale_shape_manual(values=c("water"=22)) +
  scale_fill_manual(values=c("454"="blue","Illumina"="red")) +
  guides(shape=FALSE, fill=FALSE) +
  ggtitle("Bermuda")

6:创建单独的图例:

p0 <- ggplot(data=coordinates, aes(x=longitude, y=latitude, shape=sample.nature, fill=seq.methods, color=seq.methods)) +
  geom_point(size=4) +
  scale_shape_manual("Sample:",values=c("sediment"=21,"water"=22)) +
  scale_fill_manual("Method:",values=c("454"="blue","Illumina"="red","ion torrent"="green","Sanger"="yellow")) +
  scale_color_manual("Method:",values=c("454"="blue","Illumina"="red","ion torrent"="green","Sanger"="yellow")) +
  theme(legend.key=element_rect(fill=NA))

# function to extract the legend (borrowed from: https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs )
g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

legend <- g_legend(p0)
lwidth <- sum(legend$width)

7:使用以下内容创建最终图:

grid.arrange(arrangeGrob(p1, p2, p3, p4, p5, p6, ncol=2),
             legend, widths=unit.c(unit(1, "npc") - lwidth, lwidth), nrow=1)

得出以下结果:

enter image description here