1

I'm trying to find the centroid of a list of points and then add them into a ggplot. If anyone could help--that'd be great! I'm trying to generate an animation (illustrating GPS movement across time) using the package moveVis, but having trouble with the ggplot2 sections.

Any help is greatly appreciated!

#reorder timestamps in ascending order
df<-df[order(df$timestamp),]

#remove duplicates
df <- df[!duplicated(df$timestamp),]

#convert time to time class
df$timestamp <- as.POSIXct(df$timestamp, format = '%d/%m/%y %H:%M', tz='Singapore')

m <- moveVis::df2move(df, x = "location.long", y="location.lat",            time="timestamp",
            proj ="+proj=longlat +datum=WGS84 +no_defs",  
            track_id="troop2",
            removeDuplicatedTimestamps=T)

m <- moveVis::align_move(m, res = 180 , digit = 0, unit = "mins") 

#Modify axis if necessary
ext <- extent(m) * 6


frames2 = moveVis::frames_spatial(m, trace_show=F, ext= ext, equidistant = F, map_service= "carto",
            map_type = "light") %>%
            add_labels(title= "Monitoring Survey", caption = "(2019-2020)", 
                x = "Longitude", y="Latitude") %>%
                add_timestamps(type = "label") %>%
                add_progress(colour="white") %>%
                add_northarrow(colour="white",position="bottomleft") %>%
                add_scalebar(colour="black",position="bottomright", distance=NULL, units = "km"
                )

length(frames2)
frames2[[50]]

#add roost trees 
roost_cape <- data.frame(x = c(103.786764, 103.786764, 103.787969, 103.787969, 103.786764),   #see 1st and last pair is the same 
                   y = c(1.40130129 , 1.4009375, 1.4009375, 1.40130129,1.40130129))
roost_giraffe <- data.frame(x = c(103.7888074, 103.788807, 103.789893, 103.789893, 103.7888074),   #see 1st and last pair is the same 
                    y = c(1.399994287, 1.3989033, 1.3989033, 1.39999429, 1.399994287))
roost_elephant <- data.frame(x = c(103.785098785561, 103.785099, 103.785937, 103.785937, 103.7850988),   #see 1st and last pair is the same 
                    y = c(1.402473871, 1.40191181, 1.40191181, 1.40247387, 1.402473871))

#combine into df 
roost_cape$id <- "cape"
roost_giraffe$id <-"giraffe"
roost_elephant$id <- "elephant"  
roosts_all <- rbind(roost_cape,roost_giraffe,roost_elephant)     

#customise all frames using add_gg:     
frames3 = add_gg(frames2, gg = expr(geom_path(data = roosts_all, aes(x = x, y = y, group=id), 
                                            colour = "red", linetype = "dashed")) , data = roosts_all)                                 
frames3[[100]]                                            

##### stuck. 

At this point, how do I get the midpoint / centroid or any appropriate spot to pluck the labels ($id) in?

Data

df <- structure(list(location.long = c(103.78857, 103.78835, 103.78845, 
103.78845, 103.78845, 103.78845, 103.78845, 103.78565, 103.78565, 
103.78482, 103.78504, 103.78548, 103.78546, 103.78546, 103.78897, 
103.78963, 103.78982, 103.78812, 103.78897, 103.7875), location.lat = c(1.40311, 
1.40329, 1.40374, 1.40374, 1.40374, 1.40374, 1.40374, 1.40347, 
1.40347, 1.40305, 1.40249, 1.40303, 1.40228, 1.40228, 1.4038, 
1.40381, 1.40364, 1.40377, 1.4038, 1.40351), timestamp = c("7/6/19 7:50", 
"7/6/19 8:20", "7/6/19 8:42", "7/6/19 8:50", "7/6/19 9:20", "7/6/19 9:45", 
"7/6/19 10:10", "7/6/19 16:10", "7/6/19 16:40", "7/6/19 17:10", 
"7/6/19 17:40", "7/6/19 18:10", "7/6/19 18:40", "7/6/19 19:10", 
"25/6/19 7:05", "25/6/19 7:35", "25/6/19 8:05", "25/6/19 8:20", 
"25/6/19 8:35", "25/6/19 9:05"), troop = c("Main", "Main", "Main", 
"Main", "Main", "Main", "Main", "Main", "Main", "Main", "Main", 
"Main", "Main", "Main", "Main", "Main", "Main", "Main", "Main", 
"Main"), survey_method = c("Scan", "Scan", "Ad_libitum", "Scan", 
"Scan", "Scan", "Ad_libitum", "Scan", "Scan", "Scan", "Scan", 
"Scan", "Scan", "Scan", "Scan", "Scan", "Scan", "Ad_libitum", 
"Scan", "Scan"), data_tag = c("NSF-M1a", "NSF-M1a", "NSF-M1a", 
"NSF-M1a", "NSF-M1a", "NSF-M1a", "NSF-M1a", "NSF-M1a/R1", "NSF-M1a/R1", 
"NSF-M1a/R1", "NSF-M1a/R1", "NSF-M1a/R1", "NSF-M1a/R1", "NSF-M1a/R1", 
"NSF-M1b", "NSF-M1b", "NSF-M1b", "NSF-M1b", "NSF-M1b", "NSF-M1b"
), troop2 = c("NSF", "NSF", "NSF", "NSF", "NSF", "NSF", "NSF", 
"NSF", "NSF", "NSF", "NSF", "NSF", "NSF", "NSF", "NSF", "NSF", 
"NSF", "NSF", "NSF", "NSF")), row.names = c(NA, 20L), class = "data.frame")
tjebo
  • 21,977
  • 7
  • 58
  • 94
Joan Lee
  • 91
  • 7
  • check out this thread and the linked threads from there, it should help! https://stackoverflow.com/questions/9441436/ggplot-centered-names-on-a-map/9441528#9441528 – tjebo May 10 '20 at 22:44
  • ah thank you--but this isn't a polygon though. Any tips on conversion would be great! – Joan Lee May 10 '20 at 23:40
  • I guess the centroid for a line would be calculated just like for a polygon (which is also a sort of line). From the suggested approach, you would calculate the centroid coordinates by taking the mean from the range of both x and y of each point of your lines. Unfortunatley, I don't have any experience with moveVis and its high level functions which integrate ggplot2, but I guess once you have the coordinates for the centroids, it should be fairly straight forward – tjebo May 11 '20 at 08:28

0 Answers0