Это то, что вы хотите?
library(tidyverse)
theme_set(theme_bw())
# Mean per Species
plt1 <- iris %>%
filter(Species %in% c("setosa", "virginica")) %>%
group_by(Species) %>%
mutate(mean_length = mean(Petal.Length, na.rm = TRUE),
mean_width = mean(Petal.Width, na.rm = TRUE)) %>%
ungroup() %>%
ggplot(., aes(x=Petal.Width, y=Petal.Length, color=Species)) +
geom_point() +
facet_grid(~ Species, scales = "free_x") +
geom_hline(aes(yintercept = mean_length, linetype = c("Mean length"))) +
geom_vline(aes(xintercept = mean_width, linetype = c("Mean width")),
show.legend = FALSE)
plt1 + scale_linetype_manual(NULL,
values = c(5, 3),
labels = c("Mean length", "Mean width")) +
guides(color = guide_legend(order = 1)) +
# Move legends closer to each other
theme(
legend.spacing.y = unit(0.05, "cm"),
legend.margin = margin(0, 0, 0, 0),
legend.box.margin = margin(0, 0, 0, 0))
# Mean for all Species
plt2 <- iris %>%
filter(Species %in% c("setosa", "virginica")) %>%
mutate(mean_length = mean(Petal.Length, na.rm = TRUE),
mean_width = mean(Petal.Width, na.rm = TRUE)) %>%
ggplot(., aes(x=Petal.Width, y=Petal.Length, color=Species)) +
geom_point() +
geom_hline(aes(yintercept = mean_length, linetype = c("Mean length"))) +
geom_vline(aes(xintercept = mean_width, linetype = c("Mean width")),
show.legend = FALSE)
plt2 + scale_linetype_manual(NULL,
values = c(1, 4),
labels = c("Mean length (all)", "Mean width (all)")) +
guides(color = guide_legend(order = 1)) +
theme(
legend.spacing.y = unit(0.05, "cm"),
legend.margin = margin(0, 0, 0, 0),
legend.box.margin = margin(0, 0, 0, 0))
Создано в 2018-05-23 представьте пакет (v0.2.0).