Reputation: 41
I currently have a graph that looks okay but I am trying to convert it to a line graph. However, I cannot seem to get it to work and create multiple factors like I have previously. How can I convert my first plot to a line graph with 4 lines?
This is my original graph, with the code as follows (I know there are some weird mistakes):
ggplot(data = temp, aes(x = test, y = value, shape = factor(CASE.CONTROL))) +
stat_summary(geom = "point", fun.y = "mean", size = 5, aes(col = factor(gender)))+
scale_shape_manual(name = "Case/Control", values = c(2,3))+
scale_colour_manual(name = "Gender", values = c("red","blue"))
This is what my line graph looks like so far, but obviously there is plenty wrong with it. I need to have 4 lines, as in, male/case, female/case, male/control, female/control.
ggplot(data = temp, aes(x = test, y = value, group = factor(gender),group = factor(CASE.CONTROL))) +
stat_summary(geom = "line", fun.y = "mean", size = 2, aes(group = factor(CASE.CONTROL)))+
stat_summary(geom = "line", fun.y = "mean", size = 2, aes(group = factor(gender)))+
stat_summary(geom = "line", fun.y = "mean", size = 2, aes(col= factor(gender)))
This is what the line graph looks like so far but obviously there are major issues. I must be simply missing something simple here, how can I make a line graph that shows equivalent info from the first one?
Upvotes: 1
Views: 2413
Reputation: 46908
You can create a group for the lines first to make it easier:
set.seed(100)
temp = data.frame(
test=rep(1:5,each=3,times=4),
value=rnorm(60,rep(1:4,each=15),0.5),
CASE.CONTROL = rep(c("case","control"),each=30),
gender = rep(c("M","F"),each=15,times=4)
)
temp$line_group = with(temp,paste(gender,CASE.CONTROL))
I think it's quite confusing to have 4 colors again for your different groups, but try the 2 suggestions below.
only black lines:
ggplot(data = temp, aes(x = test, y = value, shape = factor(CASE.CONTROL))) +
stat_summary(geom = "point", fun.y = "mean", size = 5, aes(col = factor(gender)))+
scale_shape_manual(name = "Case/Control", values = c(2,3))+
scale_colour_manual(name = "Gender", values = c("red","blue")) +
stat_summary(geom = "line", fun.y = "mean", size = 1,
aes(group = line_group),col="black")
Or a consistent color (I changed one of the case/control to circle so we can see it):
ggplot(data = temp, aes(x = test, y = value, shape =line_group,col=line_group)) +
stat_summary(geom = "point", fun.y = "mean", size = 5)+
scale_shape_manual(values = c(2,1,2,1))+
scale_colour_manual(values = c("red","red","blue","blue")) +
stat_summary(geom = "line", fun.y = "mean", size = 1,
aes(col=line_group))
Upvotes: 3