ggplot, geom_path, problem: some points have same coordinates

Hello everybody !

I'm currently doing the movement map of a species by time (date).
I have coordinates of one species and sometimes he return exactly to the same point after a few days. Then for one coordinates, I've multiple dates.
I've tried to use jitter function but it doesn't work when i use the path function.
Here an example of what I expect.

Hi @jonas1,

Welcome.

Is it possible for you to turn this into a reproducible example (reprex)? If you use the reprex package it is quite straightforward and having the code you've tried and some data that runs with it and illustrates the issue you are having makes it easier for people to help you with your problem.

Have you tried using both geom_path and geom_point to plot the same data? You could then jitter the points only.

Ron

Yes, excuse me, it's a mistake.

dd1 = read.csv2("data_dd1.csv")

Plot

plot = ggplot(data = dd1, aes(x = Lon, y = Lat, color = Date_J, group = as.factor(Lat)))

In addition

pj = position_jitter(h=0.0005,w=0.0005)
plot + geom_point(size=3, position=pj) +
geom_label_repel(aes(color = Date_J,label = as.factor(Date_J)), box.padding = 0.35, point.padding = 0.5, segment.color = 'grey50', segment.size = 0.5) +
geom_mark_ellipse(aes(fill = Date_J), position=pj) +
ggtitle("Movement of individu 1")

Fichier Site Date Date_J Cluster Facteurs Nombre Lat Lon
1 ARS-4_20190521_043000.wav_1934_1936.wav ARS-4 21/05/2019 21 1 ARS-42019-05-21 3 44,97272 6,418099
2 ARS-4_20190521_043000.wav_3296_3299.wav ARS-4 21/05/2019 21 1 ARS-42019-05-21 3 44,97272 6,418099
3 ARS-4_20190521_043000.wav_2784_2788.wav ARS-4 21/05/2019 21 1 ARS-42019-05-21 3 44,97272 6,418099
4 ARS-6_20190513_043800.wav_4456_4459.wav ARS-6 13/05/2019 13 1 ARS-62019-05-13 1 44,977129 6,413745
5 ARS-6_20190514_043700.wav_4304_4310.wav ARS-6 14/05/2019 14 1 ARS-62019-05-14 4 44,977129 6,413745
6 ARS-6_20190514_043700.wav_4929_4932.wav ARS-6 14/05/2019 14 1 ARS-62019-05-14 4 44,977129 6,413745
7 ARS-6_20190514_043700.wav_4995_4998.wav ARS-6 14/05/2019 14 1 ARS-62019-05-14 4 44,977129 6,413745
8 ARS-6_20190514_043700.wav_5021_5023.wav ARS-6 14/05/2019 14 1 ARS-62019-05-14 4 44,977129 6,413745
9 ARS-6_20190519_043200.wav_4731_4733.wav ARS-6 19/05/2019 19 1 ARS-62019-05-19 1 44,977129 6,413745
10 ARS-6_20190522_042900.wav_2268_2272.wav ARS-6 22/05/2019 22 1 ARS-62019-05-22 2 44,977129 6,413745
11 ARS-6_20190522_042900.wav_3955_3960.wav ARS-6 22/05/2019 22 1 ARS-62019-05-22 2 44,977129 6,413745
12 ARS-7_20190522_042900.wav_4590_4593.wav ARS-7 22/05/2019 22 1 ARS-72019-05-22 1 44,974621 6,428257
13 ARS-8_20190510_044200.wav_3847_3850.wav ARS-8 10/05/2019 10 1 ARS-82019-05-10 1 44,96993 6,4249
14 ARS-8_20190516_043500.wav_3310_3313.wav ARS-8 16/05/2019 16 1 ARS-82019-05-16 1 44,96993 6,4249
Summary

This text will be hidden

Hi,

How about this:

library("ggplot2")
library("tidyverse")
library("stringr")

dd1 = read.csv("myData.csv", stringsAsFactors = F)

#Convert string to date
dd1$Date = as.Date(dd1$Date, format = "%d/%m/%Y")

#only needed if data is read as string and with comma
dd1$Lat = str_replace(dd1$Lat, ",", "\\.") 
dd1$Lon = str_replace(dd1$Lon, ",", "\\.") #only needed if data is read as string and with comma
dd1 = dd1 %>% mutate_at(c("Lon", "Lat"), as.numeric)

#Group identical locations in one day (no way to see difference)
dd1 = dd1 %>% group_by(Date, Lat, Lon) %>% summarise() %>% arrange(Date) 
dd1 = dd1 %>% add_column(step = 1:nrow(dd1))

#Create variable to prevent label overlap
dd1 = dd1 %>% group_by(Lat, Lon) %>% mutate(moveLabel= Lon + 1:n()/1000)

#Plot the ggplot
ggplot(dd1, aes(Lon, Lat, color = Date, label = step)) + 
  geom_path(size = 2, arrow = arrow(type = "open")) +
  geom_text(aes(x = moveLabel), size = 10) +
  scale_colour_date(low = "orange", high = "purple")

Hope this helps!
PJ

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.