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

Plotting data from a csv file on a R map, highlighting their different origin

我是 R 的新手,我发现绘制这些数据非常困难 我已经获得了我的地图(使用 rworldmap)。这是我使用的两行代码:

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 为绿色 = Ion torrent

4) 黄色代表 seq.methods = Sanger

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

对于序列为 454 的水样:以蓝色作为填充色的圆圈

我被困在这一点上,据我所知,我觉得我不能再进一步了:( 在下面 link 你可以找到 .csv 文件 https://www.dropbox.com/s/t812lf6xgc0d6kf/seqmap.csv?dl=0

提前感谢您的帮助,对于任何 grammar/spelling 错误,我深表歉意,英语不是我的母语 :)

如果你可以接受字符而不是多边形,你可能想像这样尝试:

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: 阅读数据并确保他们有正确的 class:

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)

结果如下: