I need to make several copies of the same kind of plot and would like all of them to have a little arrow below the x-axis to help the reader. However, the only way I have found of placing such an arrow is the geom_segment element which requires me to specify start and end points which change according to the range of data.
Ideally, if I could access the values at which ggplot2 places tickmarks along the x-axis, that would solve my problem as I could use those as start and end points.
A minimal example is:
library(dplyr)
library(tidyr)
library(ggplot2)
data <- mtcars %>%
mutate(names = rownames(mtcars)) %>%
pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>%
select(names:val)
data1 <- mtcars %>%
mutate(names = rownames(mtcars)) %>%
filter(mpg > 15) %>%
pivot_longer(c("mpg", "cyl", "disp"), names_to = "type", values_to = "val") %>%
select(names:val)
facet_df <- data.frame(
type = "mpg"
)
plot_func <- function(df) {
df %>% ggplot +
geom_point(aes(x = val, y = names)) +
facet_wrap(facets = vars(type), ncol = 3, scales = "free_x") +
geom_segment(data = facet_df, x = 11, xend = 14, y = -1, yend = -1,
arrow = arrow(length = unit(3, "pt"))) +
coord_cartesian(clip = "off")
}
plot_func(data)
plot_func(data1)
plot_func(data)
plot_func(data1)



