0

Here's my code:

library("ggplot2") 
library("directlabels")
library("ggrepel")

gfg_data <- data.frame(x = c(2002, 2003, 2004, 2005, 2006, 
                             2007, 2008, 2009, 2010, 2011, 
                             2012, 2013, 2014, 2015, 2016, 
                             2017,2018, 2019),
                       
                       #Dinamarca/Homens       
                       Dinamarca_H = c( 74.8, 
                                        75.0,
                                        75.4,
                                        76.0,
                                        76.1,
                                        76.2,
                                        76.5,
                                        76.9,
                                        77.2,
                                        77.8,
                                        78.1,
                                        78.3,
                                        78.7,
                                        78.8,
                                        79.0,
                                        79.2,
                                        79.1,
                                        79.5),
                       #Eslováquia/Homens
                       Eslováquia_H = c(69.8,
                                        69.8,
                                        70.3,
                                        70.2,
                                        70.4,
                                        70.6,
                                        70.9,
                                        71.4,
                                        71.8,
                                        72.3,
                                        72.5,
                                        72.9,
                                        73.3,
                                        73.1,
                                        73.8,
                                        73.8,
                                        73.9,
                                        74.3),
                       #Estónia/Homens
                       Estónia_H = c(65.6,
                                     66.4,
                                     66.7,
                                     67.6,
                                     67.6,
                                     67.5,
                                     68.9,
                                     70.0,
                                     70.9,
                                     71.4,
                                     71.4,
                                     72.8,
                                     72.4,
                                     73.2,
                                     73.3,
                                     73.8,
                                     74.0,
                                     74.5),
                       #Dinamarca/Mulheres
                       Dinamarca_M = c(79.4,
                                       79.8,
                                       80.2,
                                       80.5,
                                       80.7,
                                       80.6,
                                       81.0,
                                       81.1,
                                       81.4,
                                       81.9,
                                       82.1,
                                       82.4,
                                       82.8,
                                       82.7,
                                       82.8,
                                       83.1,
                                       82.9,
                                       83.5),
                       #Eslováquia/mulheres
                       Eslováquia_M = c(77.7,
                                        77.7,
                                        78.0,
                                        78.1,
                                        78.4,
                                        78.4,
                                        79.0,
                                        79.1,
                                        79.3,
                                        79.8,
                                        79.9,
                                        80.1,
                                        80.5,
                                        80.2,
                                        80.7,
                                        80.7,
                                        80.8,
                                        81.2),
                       #Estónia/Mulheres
                       Estónia_M = c( 77.2,
                                      77.2,
                                      78.0,
                                      78.2,
                                      78.6,
                                      78.9,
                                      79.5,
                                      80.3,
                                      80.8,
                                      81.3,
                                      81.5,
                                      81.7,
                                      81.9,
                                      82.2,
                                      82.2,
                                      82.6,
                                      82.7,
                                      83.0)
                       
                       
                       
                       
)



gfg_plot <- ggplot(gfg_data, aes(x)) +  
  geom_line(aes(y = Dinamarca_H), color = "black", size = 2) +
  geom_line(aes(y = Eslováquia_H), color = "red", size = 2) +
  geom_line(aes(y = Estónia_H), color = "green", size = 2) +
  geom_line(aes(y = Dinamarca_M), color = "blue", size = 2) +
  geom_line(aes(y = Eslováquia_M), color = "purple", size = 2) +
  geom_line(aes(y = Estónia_M), color = "yellow", size = 2) +
  xlab("ano") +
  ylab("esperança média de vida em anos") 
geom_label_repel(aes(label = Dinamarca_H ),
                 nudge_x = 1,
                 na.rm = TRUE) 
  geom_label_repel(aes(label = Eslováquia_H ),
                   nudge_x = 1,
                   na.rm = TRUE) 
  geom_label_repel(aes(label = Estónia_H ),
                   nudge_x = 1,
                   na.rm = TRUE) 
  geom_label_repel(aes(label = Dinamarca_M ),
                   nudge_x = 1,
                   na.rm = TRUE) 
  geom_label_repel(aes(label = Eslováquia_M ),
                   nudge_x = 1,
                   na.rm = TRUE) 
  geom_label_repel(aes(label = Estónia_M ),
                   nudge_x = 1,
                   na.rm = TRUE)
gfg_plot

Which gives this output:

enter image description here

I want to add the labels for each line next to them, however nothing happens. I have seen Plot labels at ends of lines and https://statisticsglobe.com/add-labels-at-ends-of-lines-in-ggplot2-line-plot-r , to no avail.

A minor problem is that I also want to have every year shown in the x axis, instead of in groups of five.

Any help?

  • The "ggplot-way" to do things is to reshape your data first into a long format, then you will just have one call to `geom_line` with some group variable in the `aes` mapping to use for coloring. You need to use `aes()` if you want to get a legend. See the duplicates for alternatives. – MrFlick May 12 '22 at 13:23

0 Answers0