Search code examples
rggplot2linescatter-plotcartesian-coordinates

Tracing lines between points in a scatterplot using ggplot2 in R


You can download the data here

enter image description here

Each row represents one object that can be found in different parts, as shown in the XY coordinates (x1,y1; x2,y2;...; xn,yn). In this dataset, the rows with more points are "1", "16" and "18", with 4 points (coordinates) each.

I would like to scatterplot all of those XY coordinates using ggplot2, and to trace lines between points in each row separately.


Solution

  • I finally did it manually by running several codes:

    First, I removed columns with no data:

    Refits_H <- within(Refits_H, {
      Unit <- NULL  
      x5 <- NULL
      x6 <- NULL
      x7 <- NULL
      x8 <- NULL
      x9 <- NULL
      x10 <- NULL
      x11 <- NULL
      y5 <- NULL
      y6 <- NULL
      y7 <- NULL
      y8 <- NULL
      y9 <- NULL
      y10 <- NULL
      y11 <- NULL 
    })
    

    Second, I reorder the rows and the columns:

    Refits_H <- with(Refits_H, Refits_H[order(y4, y3, y2, y1, decreasing=FALSE),])
    Refits_H <- Refits_H[c("x4","y4","x3","y3","x2","y2","x1","y1")]
    

    And finally I answered my own question my running this code:

    library(tidyverse)
    library(dplyr)
    
    Refits_H_trans <- gather(Refits_H, xy, val) %>% 
        mutate(coord = substr(xy, 1, 1),
               pair = parse_number(xy)) %>% 
        group_by(xy) %>% 
        mutate(sample = 1:n()) %>% 
        ungroup() %>% 
        select(-xy) %>% 
        spread(coord, val) %>% 
        filter(!is.na(x) & !is.na(y))