Question Details

No question body available.

Tags

r ggplot2

Answers (2)

Accepted Answer Available
Accepted Answer
April 22, 2026 Score: 2 Rep: 35,365 Quality: High Completeness: 70%

You need to order the lines based on their distance from corall.

library(ggplot2)
library(dplyr)

corjoin %>% mutate(dist = abs(corall - corspecies)) %>% arrange(desc(dist)) %>% ggplot(aes(x = corall, xend = corspecies, y = index)) + geomsegment(aes(color = species), linewidth = 1) + geompoint(aes(x = corspecies, color = species), size = 3) + geompoint(aes(x = corall), color = "black", size = 2) + themebw()

Created on 2026-04-22 with reprex v2.1.1

April 22, 2026 Score: 0 Rep: 78,560 Quality: Medium Completeness: 80%

Include position = positiondodge(width = ) in your calls to geomsegment and one of the geompoint. In the code below I set width = 0.2 but it could be another fraction.
Also, I have simplified the rest of the code a bit. Put color = species right at the beginning and remove it from the rest of the code, it's redundant. And remove also redundant aes(x = cor
all) from geom* leaving only the one that changes to aes(x = corspecies).

library(ggplot2)

ggplot(corjoin, aes(x = corall, y = index, xend = corspecies, color = species)) + geomsegment(position = positiondodge(width = 0.2), lwd = 1) + geompoint(aes(x = corspecies), position = positiondodge(width = 0.2)) + geompoint(color = "black", size = 2)+ themebw()

enter image description here

Created on 2026-04-22 with reprex v2.1.1