Reputation: 141
I am trying to make a "spaghetti" plot in R and having trouble with geom_line and getting the grouping to work
I am evaluating prop valu for 3 languages over quartile (var index). I want a line to connect each language over index points
library(tidyverse)
index<-c(rep(1,3), rep(2,3), rep(3,3), rep(4,3))
index<-as.factor(index)
lang<-c(rep(0:2,4))
lang<-as.factor(lang)
prop <-c(0.05294181,0.09880240, 0.03294893, 0.04698709, 0.07608696,
0.04072398,0.08637001, 0.08136483, 0.06635071, 0.16041207, 0.17213115,
0.11872146)
graph<-data.frame(index,lang,prop)
ggplot(data=graph, aes(x=index, y=prop), group=lang, shape=lang) +
geom_point(shape=factor(lang), show.legend = TRUE)+geom_line()
I expect the line to connect languages over index, but instead what I am getting is lines connecting languages within index points. I am hoping someone will identify the problem and be able to let me know where I am going wrong.
Upvotes: 2
Views: 71
Reputation: 48191
The group
aesthetic needs to be called within aes
:
ggplot(data = graph, aes(x = index, y = prop, group = lang, shape = lang)) +
geom_point() + geom_line()
Upvotes: 1