I have a data set of individuals moving between different fixed locations and I want to visualise the movement between them. I know how to draw lines between the points that an individual uses (see "current" pane of attached figure).

Some individuals are repeatedly moving back and fourth between certain locations. I would like to indicate the frequency of repeated movement with varying thickness of the connectors between the dots (see "Desired" pane of figure 1). In other words: Individual 1 (red colour) is moving 2 times between point B and C (1B + 1C) and this line should be 2 times thicker than the line between point A and B which is covered 1 single time (1A).

Initial thoughts
As pointed out in a comment (on the original version of the question), the visualisation part is not the problem per se, as the thickness of the lines could be given by a "transition frequency" variable. It is the calculation of this variable that I need help with.

How do I calculate the frequency of transitions between two points?

I would prefer an R-based solution but Qgis would also be very welcome.

enter image description here

Below is a toy set making the basic plot depicted in the left pane of figure 1 (without the desired variable thickness output).

# Load necessary packages:

# Store data table with coordinates
movement <- read.table(
    "ID; X; Y
    Individual_1; 7.5; 8.5
    Individual_1; 7.5; 7.5
    Individual_1; 8.5; 7.5
    Individual_1; 7.5; 7.5
    Individual_2; 7.0; 9.0
    Individual_2; 8.0; 9.0
    Individual_2; 8.0; 8.0
    Individual_2; 9.0; 8.0"),
  sep = ";", header = T) %>%

# Convert to simple feature
movement <- movement %>% 
  st_as_sf(coords = c("X", "Y"))

# Add the coordinates as columns X and Y (used during plotting)
movement <- movement %>% 

# Plot the coordinates and connections ("movement") between them.
ggplot() +
  geom_path(data = movement, aes(x = X, y = Y, color = ID), alpha=1, linewidth=2, show.legend = F) + # paths
  geom_sf(data = movement, size = 1.5, aes(), show.legend = F) + # points
  xlim(c(7,9)) + ylim(c(7,9)) +
  • 1
    Just to get rid of an idea: I'd expect ggplot2 to handle this natively via linewidth aesthetics if your lines had some kind of a freq attribute indicating how often a path is repeated. The problem is, you're working with point data only being visualized as lines using geom_path(), as far as I understand. So first, I'd sf::st_cast() these to LINESTRINGS, probably, and use geom_sf() for visualization. Second, you need to determine freq. And maybe there is some approach/sf-extension/package existing in order to aggregate trajectories? r-spatial offers e.g. {spacetime}, {sftime}, ..
    – dimfalk
    Apr 10 at 10:11
  • 1
    Thank you for thoughtful input! Yes, I believe you are completely right. The question is rather about how to obtain the frequency of a transition. I'll rephrase the question.
    – Smerla
    Apr 10 at 11:25


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.