Connected scatterplot missing connecting lines

51 views Asked by At

This code is supposed to produce a connected scatterplot (example found here), but for some reason there are no connecting lines between the dots when I run the code with my data. What is the reason?

library(hrbrthemes)

df_long %>%
  tail(50) %>%
  ggplot( aes(x=YEAR, y=value)) +
    geom_line( color="grey") +
    geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
    theme_ipsum() +
    ggtitle("Evolution of prices")

Data

structure(list(YEAR = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 
12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 
18L, 19L, 19L), levels = c("2003", "2004", "2005", "2006", "2007", 
"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", 
"2016", "2017", "2018", "2019", "2020", "2021"), class = "factor"), 
    names = c("avg_2", "avg_1", "avg_2", "avg_1", 
    "avg_2", "avg_1", "avg_2", "avg_1", "avg_2", 
    "avg_1", "avg_2", "avg_1", "avg_2", "avg_1", 
    "avg_2", "avg_1", "avg_2", "avg_1", "avg_2", 
    "avg_1", "avg_2", "avg_1", "avg_2", "avg_1", 
    "avg_2", "avg_1", "avg_2", "avg_1", "avg_2", 
    "avg_1", "avg_2", "avg_1", "avg_2", "avg_1", 
    "avg_2", "avg_1", "avg_2", "avg_1"), value = c(9.24465000408397, 
    10.8283582089552, 9.61375824446773, 11.4962264150943, 9.57910257217093, 
    9.97674418604651, 9.55737818803894, 9.93948562783661, 9.66620683746037, 
    9.7574931880109, 10.0840478564307, 10.0276381909548, 11.0876717354968, 
    11.4456824512535, 11.3436795117236, 11.6129476584022, 12.9319600743713, 
    11.7403314917127, 13.4045649811086, 12.5052631578947, 12.5327230426866, 
    12.6259079903148, 13.1406016392921, 12.4209844559585, 12.6763227410025, 
    12.3654114365411, 12.9536953727506, 12.3433734939759, 12.9377914217035, 
    12.4618644067797, 13.6294270858362, 12.3377483443709, 14.5345731090248, 
    13.3298245614035, 14.6696202531646, 14.1116005873715, 16.6562338726815, 
    15.1701030927835)), row.names = c(NA, -38L), class = c("tbl_df", 
"tbl", "data.frame"))
1

There are 1 answers

2
Erik De Luca On

You have to mutate YEAR as numeric but you have to get the levels, not the values thus as.numeric(levels(YEAR))[YEAR] and add in aes group = names.

df_long %>%
  tail(50) %>%
  mutate(YEAR = as.numeric(levels(YEAR))[YEAR]) %>%
  ggplot(aes(x=YEAR, y=value, group = names)) +
  geom_line( color="grey") +
  geom_point(shape=21, color="black", fill="#69b3a2", size=6) +
  theme_ipsum() +
  ggtitle("Evolution of prices")

enter image description here