I want to plot the rows of the following matrix in ggplot as a line plot.
Specifically:
1) I want 25th Pct Cohort 1, 50th Pct Cohort 1 and 75th pct Cohort 1 colored black
2) I want 25th Pct Cohort 2, 50th Pct Cohort 2, and 75th pct Cohort 2 colored steelblue
3) I want 25th Pct Cohort 3, 50th Pct Cohort 3, and 75th pct Cohort 3 colored grey
4) I want all 50th pct lines a shade darker or slightly larger in size (so they stand out).
5) I want a legend that labels each line according to the rownames
6) I want all 25th pct linetypes to be dotted
7) I want all 50th pct linetypes to be solid
8) I want all 75th pct linetypes to be long-dash
Sorry for all the requirements. I'm new to this and learning.
start = as.Date("1993-12-01")
end = as.Date("2018-09-01")
dates = seq(from = start, to = end, by = "quarter")
test <- matrix(nrow=9, ncol =100, rnorm(900,0,1))
colnames(test) = as.character(dates)
rownames(test) = c("25th Pct Cohort 1", "50th Pct Cohort 1", "75th Pct Cohort 1", "25th Pct Cohort 2", "50th Pct Cohort 2" , "75th Pct Cohort 2", "25th Pct Cohort 3", "50th Pct Cohort 3", "75th Pct Cohort 3")
The dataset doesn't need to be reproducible. Its just teaching me the process.
I understand the first step is to convert from wide to long format. I do this as follows:
library(reshape)
df <- melt(as.matrix(test))
df <- melt(as.matrix(test))
colnames(df) <- c("Cohort", "Date", "value")
df$Date <- as.Date(df$Date)
ggplot(df, aes(x=Date, y= value)) + geom_line(aes(colour = Cohort)) + theme_classic() +
scale_colour_manual("",
values = c("25th Pct Cohort 1" = "black", "50th Pct Cohort 1" = "black", "75th Pct Cohort 1" = "black", "25th Pct Cohort 2" = "steel blue", "50th Pct Cohort 2"= "steelblue" , "75th Pct Cohort 2" = "steelblue", "25th Pct Cohort 3" = "grey", "50th Pct Cohort 3" = "grey", "75th Pct Cohort 3" = "grey"),
breaks = c("50th Pct Cohort 1", "75th Pct Cohort 1", "25th Pct Cohort 2", "50th Pct Cohort 2" , "75th Pct Cohort 2", "25th Pct Cohort 3", "50th Pct Cohort 3", "75th Pct Cohort 3")) +
scale_linetype_manual("",
values = c("dotted", "solid", "longdash", "dotted", "solid", "longdash", "dotted", "solid", "longdash"),
breaks = c("50th Pct Cohort 1", "75th Pct Cohort 1", "25th Pct Cohort 2", "50th Pct Cohort 2" , "75th Pct Cohort 2", "25th Pct Cohort 3", "50th Pct Cohort 3", "75th Pct Cohort 3"))
But i am lost after this.