Home > Software engineering >  Unexpected crossing geom_segment in ggplot
Unexpected crossing geom_segment in ggplot

Time:12-25

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

CodePudding user response:

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.

  • Related