【问题标题】:Plotting data from a csv file on a R map, highlighting their different origin在 R 地图上绘制 csv 文件中的数据,突出显示它们的不同来源
【发布时间】:2016-01-12 19:02:38
【问题描述】:

我是使用 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 样品性质=沉积物

样品性质的圆圈=水

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

1) seq.methods 的 RED = Illumina

2) seq.methods = 454 的蓝色

3) seq.methods 的绿色 = Ion torrent

4) seq.methods = Sanger 的黄色

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

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

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

提前感谢您的帮助,并对任何语法/拼写错误深表歉意,英语不是我的母语 :)

【问题讨论】:

    标签: r plot maps rworldmap


    【解决方案1】:

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

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

    【讨论】:

    • 您好,lukeA,感谢您的帮助!我无法完全理解第二部分,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) # 在 Windows 上打开 pdf doc 以轻松缩放/平移...它只是向我展示pdf格式的世界地图(
    • 点应该在那里,但非常小。也许将cex 放回cex=1(或更大)和/或减小widthheight 中的文档尺寸,以便将它们更多地挤压在一起?还是我弄错了?
    • 不,我重新运行它,它在这里工作。正如我所说,您可能需要使用widthheightcexalpha.f,它们控制点的透明度(1 = 实心,0 = 透明)。此外,使用resolution="high" 看起来更好。 :)
    • 读取 .csv 时出现了一个小问题,已解决 :) 我对参数进行了一些调整,效果非常好!感谢您的帮助
    【解决方案2】:

    由于您的数据点遍布世界各地,当您在一张地图上绘制所有内容时,可能很难区分地图上的点。作为替代方案,您可以创建一个多面图,其中包含世界多个地区的特写镜头。我建议以下步骤:

    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)
    

    给出以下结果:

    【讨论】:

    • 谢谢 Jaap,这个集群版本非常适合这种情况!您的回答的速度和详细程度给我留下了深刻的印象,如果这不是问题,我想问您如何概括这个示例,换句话说:如果我有一个非常扩展的 .csv 格式的数据集(大约 600- 800 个位置)如何制作世界地图(非集群)?还使用@lukeA给出的指示,我能够以pdf格式获得世界地图,但在绘图阶段出现问题......地图上没有显示这些点,我无法添加传说:(你能告诉我步骤吗?
    • 问题解决了,我的完整脚本中只有几个问题。我在使用 R 方面还是新手,但正在努力改进 :) 再次感谢您
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2022-07-22
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多