一种解决问题的方法是将点的坐标和形状放入一个辅助数据框
df_points
中,并在
geom_point
和
geom_text
中使用它。
至于线条,将数据从宽格式
转换为长格式,然后调用一次
geom_line
即可。设置参数
inherit.aes = FALSE
,对于
geom_point
还要设置
show.legend = FALSE
。
library(ggplot2)
library(dplyr)
library(tidyr)
df_points <- data.frame(x = rep(0, 3),
y = c(0, 1/6, 0.126),
shape = factor(c("square", "circle", "triangle"),
levels = c("square", "circle", "triangle")))
df %>%
pivot_longer(
cols = starts_with('a'),
names_to = 'y',
values_to = 'a'
) %>%
ggplot(aes(tau_3, a, color = y)) +
geom_line() +
geom_point(data = df_points,
mapping = aes(x, y, shape = shape),
size = 5,
show.legend = FALSE,
inherit.aes = FALSE) +
geom_text(data = df_points,
mapping = aes(x, y, label = shape),
vjust = -1.5, hjust = 0,
inherit.aes = FALSE) +
xlim(0,0.6) +
ylim(0,0.4) +
scale_color_manual(name = "Legend",
values = c("blue", "green"),
labels = c("GLO", "GEV")) +
scale_shape_manual(values = c("square", "circle", "triangle"))
测试数据
set.seed(2020)
n <- 20
tau_3 <- runif(n, 0, 0.6)
a1 <- runif(n, 0, 0.4)
a2 <- runif(n, 0, 0.4)
df <- data.frame(tau_3, a1, a2)