如何使用ggplot2手动添加图例

时间:2014-10-05 05:20:25

标签: r ggplot2

如何在图片中手动添加图例。我试图在网上找到答案,但未能找到合适的答案。请指教。

提前感谢您的帮助。

p = ggplot(gData, aes(seq, mue)) + geom_line(aes(x = seq, y = mue), lty=1, lwd=1) + xlim(-10, 10) + ylim(-1.0, 1.0)  + 

xlab(expression(paste(u[t-1],sep = ""))) + ylab(expression(paste(mu[t],""))) + 

geom_line(aes(x = seq, y = se1), type="b", lty=2, lwd=1.1) + geom_line(aes(x = seq, y = se2), type="b", lty=2, lwd=1.1) + 

theme(axis.title.x = element_text(face = "bold", color = "black", size = 14), 

axis.title.y = element_text(face = "bold", color = "black", size = 14)) 

1 个答案:

答案 0 :(得分:0)

您可以使用theme(legend.position=c(num, num))手动选择图例位置。考虑到OP的评论,我采取了以下方法。

# Sample data
seq <- rep(c(1:10), times = 3)
value <- rep(c(10,9,6,5,3,2), each = 5)
category <- rep(c("se1", "me", NA), each = 10)
foo <- data.frame(seq, value, category, stringsAsFactors = FALSE)

ggplot(data = foo, aes(x = seq, y = value, color = category)) +
geom_line(lty=1, lwd=1) +
theme(legend.position=c(.8, .5)) +
scale_x_continuous(breaks = seq)

enter image description here