0

For a reason I simply cannot understand, my geom_segment is crossing over for my first two y variables, and behaving as expected for the rest. Short of posting my data frame, can anyone work out how this might even happen to begin with? The geom_points are correct for both variables.

data %>% 
    ggplot() +
    aes(y = fct_reorder2(lang, year, -mean), x = mean) +
  geom_errorbar(data = twenty16,
                xmin = twenty16$mean - twenty16$ci, 
                xmax = twenty16$mean + twenty16$ci,
                width = 0.4,
                colour = "#aeb6bf")+
    geom_point(colour = factor(year), size = 4) +
    theme_minimal()+
    theme(text = element_text(family = "hel" ),
          axis.text = element_text(size = 12),
          axis.title = element_text(size = 15),
          title = element_text(size = 15))+
    geom_segment(data = twenty06,
                 arrow = arrow(length = unit(0.02, "npc"), type ="closed"),
                 lineend = 'round',
                 linejoin = "mitre",
                 aes(
                 x = mean, 
                 y = lang, 
                 xend = twenty16$mean, 
                 yend = twenty16$lang)
                 )+
  labs(title = "Percent of learners reaching the low international Benchmark \n      by language, 2006 to 2016 with 90% CIs",
       y = "Languages",
       x = "Percent",
       legend = "Year")

unexpected crossing over geom_segment

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 2
    Check your `twenty06` and `twenty16` data frames. The error is likely there because you are calling two different data frames in the same `geom` call. Perhaps a better flow is to join the two data frames together prior to creating the chart. – Phil Dec 21 '22 at 16:12
  • 1
    Otherwise we would need an example of your data in order to reproduce. – Phil Dec 21 '22 at 16:13
  • 6
    It's really hard to know without a [reproducible example](https://stackoverflow.com/q/5963269/5325862)—it's a question about the setup of your data which we don't have. You could focus on what's needed for the segments. Best guess I could make is that you're using `$` inside `geom_segment` for some reason instead of supplying aesthetics via `aes` – camille Dec 21 '22 at 16:13
  • 3
    Don't ever use `$` inside an `aes()` It doesn't allow ggplot to reorder things properly. You should make a custom data object for your `geom_segment` layer instead. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Dec 21 '22 at 16:13
  • 2
    Was about to say exactly what Phil said. It's much better practice to merge or join all the data for the plot into one frame before calling `ggplot()`. I also think it's better to define all layer-independent `aes()` variables in the `ggplot()` call rather than in individual `geom_xyz()` calls too; only put things that are layer-specific in the latter. – Curt F. Dec 21 '22 at 16:14

1 Answers1

0

I swapped the order of English and Afrikaans in the twenty16 data frame and fixed the problem. Why this worked when fct_reorder of the main dataset worked for the other languages, I have no idea. Perhaps someone can provided a more comprehensive answer based on this solution.