Reputation: 11
I want to build a single graph displaying several outcomes (with different point- and lineshapes for each, respectively) for several strata (displayed in different colours) over time. Using this for one group works:
data <- data.frame(
time = rep(c("Baseline", "Follow-Up 1", "Follow-Up 2"), each = 8),
stratum = rep(c("Intervention", "Control"), 12),
outcome = rep(c("Sensitivity", "Specificity", "PPV", "NPV"), 3, each = 2),
value = runif(24)
)
# working
data %>%
filter(stratum == "Intervention") %>%
ggplot(aes(x = time, y = value, group = outcome, colour = stratum)) +
geom_point(aes(shape = outcome)) +
geom_line(aes(linetype = outcome))
# not working
data %>%
ggplot(aes(x = time, y = value, group = outcome, colour = stratum)) +
geom_point(aes(shape = outcome)) +
geom_line(aes(linetype = outcome))
If I want the same for both strata it does not and produces following error:
Error in `f()`:
! geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
Run `rlang::last_error()` to see where the error occurred.
The info in last_error() does not help me. Has anyone a solution here?
Upvotes: 1
Views: 542
Reputation: 146050
The group
aesthetic should uniquely define the points that you want connected with a line. You need to consider both outcome and stratum in that.
data %>%
ggplot(aes(x = time, y = value, group = paste(outcome, stratum), colour = stratum)) +
geom_point(aes(shape = outcome)) +
geom_line(aes(linetype = outcome))
Upvotes: 1