'Strange line connection using ggplot geom_line colours

A problem occurs when i add a colour(gradient) into the aesthetics of geom_line. It seems that the dots are no longer connected by a line but instead transformed into squares, which messes up the plot. Does anyone have an idea how to solve this problem or if this is how R or ggplot handles colour(gradients). This problem doesn't occur, when i remove the colours from the aes().

Thanks in advance!

with colourwith colour2without colourwithout colour2simple with coloursimple without colour

Simple code:

df <- data.frame(c(1,3,4,2,5,8),c(1,2,3,4,5,6))
colnames(df) <- c("x","y")

ggplot()+
  theme_classic()+
  geom_line(data=df,aes(x=x,y=y,colour=y),size=2)

ggplot()+
  theme_classic()+
  geom_line(data=df,aes(x=x,y=y),size=2) 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source