Reputation: 764
I have three different data set,
> dput(logit_mean)
structure(c(1.34189585608394, 0.847629766112133, 0.502984724184437,
0.293946235601552, 0.206499805664176, 0.226630939673008, 0.340325142928746,
0.533567920732087, 0.792344778383729, 1.10264122118437, 2.10410207183928,
2.39007043292364, 2.7472025599441, 3.13582023485062, 3.51624523959319,
3.84879935612177, 4.09380436638634, 4.21158205233687, 4.16245419592333,
3.9067425790957, 1.46025341360241, 2.14156603885982, 2.75516433443725,
3.29118077503553, 3.7397478353555, 4.09099799009798, 4.3350637139638,
4.46207748165381, 4.46217176786882, 4.32547904730968), .Dim = c(10L,
3L))
> dput(logit_LCL_mean)
structure(c(1.05351660368681, 0.628916609291152, 0.258130717327059,
0.00463564423558981, -0.118711150605381, -0.119775198249695,
-0.00943890367383587, 0.2120834094038, 0.516104794995947, 0.771900259001094,
1.728942625138, 1.93004332451069, 2.1051342500183, 2.32579479415003,
2.55055241931885, 2.774582433554, 2.95504501785115, 3.07188155790784,
3.06353387892773, 2.81246169848574, 1.23766486839515, 1.92345141391083,
2.49077865317756, 2.98569639891182, 3.39229201620524, 3.69476128054215,
3.89382753174118, 4.02023529154679, 4.00128330320072, 3.69675304553835
), .Dim = c(10L, 3L))
> dput(logit_UCL_mean)
structure(c(1.65351862117267, 1.07141678565209, 0.727549044031012,
0.528002186532385, 0.457316509237024, 0.495293082125037, 0.621734110908969,
0.803998291666232, 1.04499958137129, 1.44068951524647, 2.49956079692368,
2.76677000672866, 3.22892029161779, 3.73203241679821, 4.22148180147624,
4.65402060247669, 4.99516431524168, 5.21764445415144, 5.35973642037135,
5.50118639749062, 1.6808252495808, 2.3732336294389, 3.05627843729075,
3.64261086868091, 4.15079922999075, 4.55990311955228, 4.83697444552739,
4.94477232896115, 4.95551580166221, 5.06294719673336), .Dim = c(10L,
3L))
I would like to create the following plot with ggplot2. I can do it without ggplot2 using the following code
plot(logit_mean[,1]~time, type="n", ylab="",
ylim=c(min(logit_mean,logit_LCL_mean,logit_UCL_mean),
max(logit_mean,logit_LCL_mean,logit_UCL_mean))
,
main="Logit functions", cex.main=1, font.main=1
)
col.set=c("green", "blue", "purple", "deeppink",
"darkorchid","darkmagenta","black","khaki")
for ( g in 1:ncol(logit_mean)){
lines(logit_mean[,g]~time, col=col.set[g])
lines(logit_LCL_mean[,g]~time, col=col.set[g], lty=2)
lines(logit_UCL_mean[,g]~time, col=col.set[g], lty=2)
}
legend("topleft", inset=0.05,legend=paste0("Clus_", 1:ncol(logit_mean)),
col=col.set, lty=1, box.lty=0, cex=0.8)
How can I do it with ggplot2. Any help is appreciated. The plot must be similar to this picture
This is what I tried on the first data set. How can include other two in the same plot
dat=data.frame(tm=c(1:10),logit_mean)
dat_long=melt(dat,id="tm")
col_set=c("green", "blue","green")
ggplot(data=dat_long,
aes(x=tm, y=value, colour=variable)) +
geom_line()+scale_color_manual(values=col_set)+theme_bw()
Upvotes: 0
Views: 72
Reputation: 174478
The key here is to get your data into a single data frame in long format, with a single column for mean, and a column each for upper and lower confidence intervals. You then need to add a column labelling the clusters as well as one for time:
df <- data.frame(mean = as.vector(logit_mean),
lower = as.vector(logit_LCL_mean),
upper = as.vector(logit_UCL_mean),
clus = rep(c("clus_1", "clus_2", "clus_3"), each = 10),
time = rep(1:10, 3))
The plot itself is straightforward. I have added some styling to make it more like your base R example:
ggplot(df, aes(time, mean, colour = clus)) +
geom_line() +
geom_line(aes(y = upper), linetype = 2) +
geom_line(aes(y = lower), linetype = 2) +
scale_colour_manual(values = c("green", "blue", "purple")) +
labs(title = "Logit functions", y = "") +
theme_classic() +
theme(legend.position = c(0.15, 0.75),
plot.title.position = "plot",
plot.title = element_text(hjust = 0.5, size = 16))
Though personally I think i's much clearer as a geom_ribbon
:
ggplot(df, aes(time, mean, colour = clus)) +
geom_line() +
geom_ribbon(aes(ymin = lower, ymax = upper, fill = clus),
alpha = 0.1, linetype = 2) +
scale_fill_manual(values = c("green", "blue", "purple")) +
scale_colour_manual(values = c("green", "blue", "purple")) +
labs(title = "Logit functions", y = "") +
theme_bw() +
theme( plot.title.position = "plot",
plot.title = element_text(hjust = 0.5, size = 16))
Upvotes: 2