Geom_text overplotting

Hi there! I have a datarame like this:

datos[1:20,]
# A tibble: 20 x 4
  country_name country_code year  value
1 Argentina    ARG          1990  1    
2 Bolivia      BOL          1990  1    
3 Brazil       BRA          1990  1    
4 Chile        CHL          1990  1    
5 Colombia     COL          1990  1    
6 Ecuador      ECU          1990  1    
7 Paraguay     PRY          1990  1    
8 Peru         PER          1990  1    
9 Uruguay      URY          1990  1    
10 Argentina    ARG          1991  1.08 
11 Bolivia      BOL          1991  1.03 
12 Brazil       BRA          1991  0.997
13 Chile        CHL          1991  1.06 
14 Colombia     COL          1991  1.00 
15 Ecuador      ECU          1991  1.02 
16 Paraguay     PRY          1991  1.01 
17 Peru         PER          1991  1.00 
18 Uruguay      URY          1991  1.03 
19 Argentina    ARG          1992  1.15 
20 Bolivia      BOL          1992  1.03 

And I have a chart like this.

grafico<- ggplot(datos,aes(x=year,y=value,group=country_name, color = 
country_name,label=country_code)) + 
  geom_line(size=1.25,color=gris) + 
  geom_point(color=gris)+
  geom_text(data = . %>% group_by(country_name) %>% filter(year==max(year)), 
          nudge_x=0.1, hjust=0.5,vjust=(-0.5),color=gris) +
  expand_limits(x = as.numeric(max(datos$year))-as.numeric(min(datos$year))+1) +
  guides(colour=FALSE)+
  scale_x_discrete(breaks=c(1990,1995,2000,2005,2010,2015,2019))

There is any way I can avoid the overplotting of each line's labels? At the bottom, Brazil and Ecuador are overplotting. I tried using position="dodge" in geom_text but it is not working.

You can try geom_text_repel() from the ggrepel package

library(tidyverse)
library(ggrepel)

# Sample data on a copy/paste friendly format
datos <- data.frame(
  stringsAsFactors = FALSE,
      country_name = c("Argentina","Bolivia",
                       "Brazil","Chile","Colombia","Ecuador","Paraguay","Peru",
                       "Uruguay","Argentina","Bolivia","Brazil","Chile",
                       "Colombia","Ecuador","Paraguay","Peru","Uruguay",
                       "Argentina","Bolivia"),
      country_code = c("ARG","BOL","BRA","CHL",
                       "COL","ECU","PRY","PER","URY","ARG","BOL","BRA",
                       "CHL","COL","ECU","PRY","PER","URY","ARG","BOL"),
              year = c(1990L,1990L,1990L,1990L,
                       1990L,1990L,1990L,1990L,1990L,1991L,1991L,1991L,
                       1991L,1991L,1991L,1991L,1991L,1991L,1992L,1992L),
             value = c(1,1,1,1,1,1,1,1,1,1.08,
                       1.03,0.997,1.06,1,1.02,1.01,1,1.03,1.15,1.03)
)

ggplot(datos,aes(x = year, y = value, group = country_code, label = country_code)) + 
    geom_line(size=1.25,color="grey") + 
    geom_point(color="grey")+
    geom_text_repel(data = . %>% group_by(country_name) %>% filter(year==max(year)), 
              nudge_x=0.1, hjust=0.5,vjust=(-0.5),color="grey") +
    scale_x_discrete(breaks=c(1990,1995,2000,2005,2010,2015,2019))

Created on 2020-08-08 by the reprex package (v0.3.0)

BTW, next time please share sample data on a copy/paste friendly format, here is how to do it

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

Thank you! Next time i will!