Home > Mobile >  geom_text in ggplot does not show complete data
geom_text in ggplot does not show complete data

Time:11-01

My data:

  dat <- structure(list(Country = c("Rwanda", "Uganda", "Honduras", "Slovenia", 
                                    "Bosnia and Herzegovina", "Rwanda", "Slovenia", "El Salvador", 
                                    "Bosnia and Herzegovina", "Slovakia", "Lithuania", "Trinidad and Tobago", 
                                    "Rwanda", "Honduras", "Uganda", "Lithuania", "Poland", "Slovakia", 
                                    "Finland", "Serbia"), Year = c("2020", "2020", "2020", "2020", 
                                                                   "2020", "2030", "2030", "2030", "2030", "2030", "2040", "2040", 
                                                                   "2040", "2040", "2040", "2050", "2050", "2050", "2050", "2050"
                                    ), x = c(3.52229269054852, 3.48719818221878, 3.27479730257771, 
                                             3.23749207619133, 3.21860068070842, 4.03311621112984, 3.84576854280025, 
                                             3.59225623413964, 3.53162905338438, 3.49789347217355, 3.66601597889191, 
                                             3.65257647482563, 3.65087176257351, 3.52944633458076, 3.48476020371068, 
                                             4.97433423368669, 4.91067633570393, 4.72467884942941, 4.72059996530178, 
                                             4.66345169203987)), row.names = c(NA, -20L), class = c("tbl_df", 
                                                                                                    "tbl", "data.frame"))
  library(ggplot2)
  
  ggplot(dat, aes(x = Year, y = x, label = factor(Country)))  
    geom_text(check_overlap = TRUE) 

Why does each of my Year does not show 5 countries. Except 2030, all years do not show all the countries. Each year has 5 countries.

enter image description here

CodePudding user response:

You can use ggrepel::geom_text_repel:

ggplot(dat)  
  ggrepel::geom_text_repel(aes(x = Year, y = x, label = factor(Country)))

output: enter image description here

  • Related