请考虑以下 R 脚本(取自此处并稍作修改):
require(ggplot2)
x <- 1:10
y <- jitter(x^2)
DF <- data.frame(x, y)
p <- ggplot(DF, aes(x = x, y = y)) + geom_point() +
stat_smooth(method = 'lm', aes(colour = 'linear')) +
stat_smooth(method = 'lm', formula = y ~ poly(x,2),
aes(colour = 'polynomial')) +
stat_smooth(method = 'nls', formula = y ~ a * log(x) +b,
aes(colour = 'logarithmic')) +
stat_smooth(method = 'nls', formula = y ~ a*exp(b *x),
aes(colour = 'Exponential')) +
theme(legend.position = "top")
p <- p + guides(guide_legend(ncol=2,nrow=2,byrow=TRUE))
p
Run Code Online (Sandbox Code Playgroud)
图例显示在图的顶部。我想把这个图例分成两行,每行有两个键。这可能吗?
请注意,如您所见,我已经尝试过
p+guides(guide_legend(ncol=2,nrow=2,byrow=TRUE))
Run Code Online (Sandbox Code Playgroud)