Soren Christensen
Soren Christensen

Reputation: 376

line in ggplot connects all dots and not only those in group

I have molten dataframe and wants to plot all the different variables with a line. However when i do that GGplot connects all lines and the plot is meaningless.

df <-data.frame (names =c("a","b","c","a","b","c"),
   time = c(1,1,1,2,2,2),
   xvar = c(150,37,38,150,50,50))

ggplot(df,aes(x=time, y=xvar), group = names)+
  geom_line()

Can this be fixed, so the plot shows three lines connecting the dots in each variable

Upvotes: 0

Views: 155

Answers (1)

cymon
cymon

Reputation: 413

You put the group outside of the aes(). It needs to be inside the aes():

ggplot(df,aes(x=time, y=xvar, group = names)) +
  geom_line()

Upvotes: 1

Related Questions