'Annotate a point for different facet plots
I want to annote a point onto a bar-plot for each independent facet plot. However, when I use annotate it annotates both plots simultaneously at the same points. How can I specify a point annotation different to each plot?
Here's my data and code:
data = structure(list(training = c("NBack", "NBack", "NBack", "NBack",
"NBack", "NBack", "NBack", "NBack", "NBack", "NBack", "NBack",
"Speed", "Speed", "Speed", "Speed", "Speed", "Speed", "Speed",
"Speed", "Speed", "Speed"), Scores = c(1, 3, 4, 4, 0, 1, -4,
-1, -2, 1, 2, 3, -3, -1, 1, 1, -4, -2, 3, -2, 3)), row.names = c(NA,
-21L), class = c("tbl_df", "tbl", "data.frame"))
ggplot(data, aes(x = scores, colour = factor(training),
stat="identity")) +
geom_bar(fill="white") +
facet_wrap(~training, scales="free_x") +
labs(colour='Training', x="Score differences", y = "Frequency of differences") +
annotate("point", x = 1, y = 4, size = 2, colour = "black")
Solution 1:[1]
Just add another geom_point with another table containinga nnotation coordinates:
library(tidyverse)
data <- structure(list(training = c(
"NBack", "NBack", "NBack", "NBack",
"NBack", "NBack", "NBack", "NBack", "NBack", "NBack", "NBack",
"Speed", "Speed", "Speed", "Speed", "Speed", "Speed", "Speed",
"Speed", "Speed", "Speed"
), Scores = c(
1, 3, 4, 4, 0, 1, -4,
-1, -2, 1, 2, 3, -3, -1, 1, 1, -4, -2, 3, -2, 3
)), row.names = c(
NA,
-21L
), class = c("tbl_df", "tbl", "data.frame"))
annotations <- tribble(
~training, ~Scores, ~y,
"NBack", 1, 4,
"Speed", 2, 5,
)
ggplot(data, aes(
x = Scores, colour = factor(training),
stat = "identity"
)) +
geom_bar(fill = "white") +
geom_point(
data = annotations,
mapping = aes(y = y), # add lacking aesthetics
color = "black",
size = 3
) +
facet_wrap(~training, scales = "free_x") +
labs(
colour = "Training", x = "Score differences",
y = "Frequency of differences"
)

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | danlooo |
