再来个 Base R 版,去掉所有额外包的依赖,打擂:
df <- transform(
expand.grid(
x = seq(0.1, 10, by = 0.1),
z = 1:4),
y = z / x)
plot(df$x[df$z == i], df$y[df$z == i], type = 'l', xlab = 'x', ylab = 'y')
for(i in 2:4) lines(df$x[df$z == i], df$y[df$z == i], col = i)
legend('topright', legend = 1:4, col = 1:4, lty = 1, title = 'z')
以及 lattice 版:
lattice::xyplot(y~x, groups = z, data = df, type = 'l', ylim = c(0, 10), auto.key = list(corner = c(1, 1), points = FALSE, lines = TRUE))
相比之下,ggplot2 确实优雅。