I am trying to add the legends to the several geom_point. Although, as I have three geom_points the labels appear only for one variable ("Outcome").
Additionally to the "Outcome" variable, I want to show the labels for two diamonds: the blue diamond ("TStartTime") and the green ("indicator").
# Code for numbers reproduction
df <- data.frame(subjectID = factor(1:10, 10:1),
stage = rep(c("treated"), times = c(10)),
endTime = c(6, 8, 3, 5, 10, 14, 2, 12, 6, 6),
Outcome = rep(c("healthy", "disability", "healthy", "disability", NA, NA, NA, NA, "healthy", "disability"), 1),
TStartTime=c(1.0, 1.5, 0.3, 0.9, NA, NA, NA, NA, NA, NA),
TEndTime=c(6.0, 7.0, 1.2, 1.4, NA, NA, NA, NA, NA, NA),
TimeZero=c(0,0,0,0,0,0,0,0,0,0),
ind=rep(c(!0, !0, !0, !0, !0), times = c(2, 2, 2, 2, 2)),
Garea=c(1.0, 1.5, 0.3, 0.9, 2, 2, NA, NA, NA, NA),
indicator=c(NA, NA, NA, NA, 4, 1, 5, 2, NA, NA))
# Code for the plot
gg <- ggplot(df, aes(subjectID, endTime)) +
scale_fill_manual(values = c("khaki", "orange")) +
geom_col(aes(fill = factor(stage))) +
geom_point(data=df, aes(subjectID, TStartTime), colour = c("blue"), fill =alpha(c("#FAFAFA"), 0.2), shape=18, size=4) +
coord_flip() + # blue diamond
geom_point(data=df, aes(subjectID, indicator), colour = c("green"), shape=18, size=4) +
coord_flip() + # green diamond for indicator
geom_point(aes(colour = Outcome, shape = Outcome), size = 4) +
coord_flip() +
scale_colour_manual(values = c('purple','gray'), na.translate=FALSE) +
scale_y_continuous(limits = c(-0.2, 15), breaks = 0:15) +
labs(labels= "",
x = "ID ",
fill = "Status",
y = "Days",
title = "Plot") +
theme_classic()
theme(plot.title = element_text(hjust = 0.5),
plot.caption = element_text(size = 7, hjust = 0))