I want to create a ggplot2 with multiple lines with different line types and colors. I think I know how to change the color, but I keep having a hard time changing the line type.
please refer to the below code and explanation.
Multi_Export <- read.csv("Multi_Regression - Student.csv", header = TRUE)
Multi_Export[,'CPTED_YR'] <-factor(Multi_Export[,'CPTED_YR'])
Multi_2000m <- Multi_Export %>% filter(CPTED_YR == '2'|CPTED_YR == '1'|CPTED_YR == '-1'|CPTED_YR == '-2')
SP <- Multi_2000m %>% ggplot(aes(x=CPTED_DIST, y=CPI_Price, color = CPTED_YR, group = CPTED_YR)) + geom_smooth(method="loess")
SP + geom_vline(xintercept=1000) + coord_cartesian(ylim = c(7000, 12000))
This is what I get when I use the code on the top. As you can see, I have 3 variables; x=distance, y=price, and 4 line groups of CPTED_YR = -2, -1, 1, 2
From here, I want to change the color and the line type, not automatically, but in the following order:
CPTED_YR = -2, longdash (5), blue
CPTED_YR = -1, dashed (2), navy
CPTED_YR = 1, solid (1), red
CPTED_YR = 2, twodash (6), burgundy
so this is the color code that I tried.
Multi_2000m <- Multi_Export %>% filter(CPTED_YR == '2'|CPTED_YR == '1'|CPTED_YR == '-1'|CPTED_YR == '-2')
SP <- Multi_2000m %>% ggplot(aes(x=CPTED_DIST, y=CPI_Price, color = CPTED_YR)) + geom_smooth(method="loess")
SP + geom_vline(xintercept=1000) + coord_cartesian(ylim = c(7000, 12000)) + scale_color_manual(values = c("#DC71FA","#FC717F","#7997FF", "#00BBDB"))
And this is what I got. Seemed to work fine for now.
The real problem is adding the line type. Based on the suggestion from the link at the bottom, I saw that I had to define the "group=CPTED_YR" first and then move on to "geom_line", so that is what I did.
Link: Different line types in one ggplot graph
SP + geom_vline(xintercept=1000) + coord_cartesian(ylim = c(7000, 12000)) + scale_color_manual(values = c("#DC71FA","#FC717F","#7997FF", "#00BBDB"))
+ geom_line(aes(linetype=CPTED_YR))
However, this is what I got. A total catastrophe. I have suffered it once when I forgot to change the CPTED_YR as text (string) not a number (integer). This time, I didn't forget to change it, so I think something else is causing an issue right now.
Please let me know how to change the line type, and if I can set each line type manually, also please tell me how to do so.