我有一个表格的直方图
rm(list = ls())
set.seed(1)
x1 <- rnorm(100, mean=1)
x2 <- rnorm(100)
hist(x1, col="black", ylim=c(0,30), xlim=range(pretty(range(x1, x2))), xlab="x-axis label", ylab="y-axis label", main="", cex.lab=1.3, las=1)
hist(x2, xlab="", ylab="", main="", ylim=c(0,yhigh), xlim=c(0,.05), density = 20, col= "gray", axes=F, add=TRUE, lty=1)
lines(density(x2, from = 0, to = max(x2)), col ="firebrick", lwd = 1.5)
legend("topright", c("group1", "group2", "density"), lty=c(1,1, 1), bty = "n")
我无法正确显示图例,使用实心黑条,阴影条和红线来匹配数据(我知道密度线不对,但包含以便有一条线在这个例子中)。有人知道怎么做吗?
答案 0 :(得分:0)
这是你在找什么?
legend("topright",
c("group1", "group2", "density"),
lty=c(1, 2, 1),
col=c("black","gray","firebrick"),
bty = "n")
答案 1 :(得分:0)
legend("topright", c("group1", "group2", "density"),
lty=c(1,2, 1), bty = "n",
fill=c("black", "gray", "firebrick"))
已编辑:包含角度和密度参数以及相应的值
legend("topright", c("group1", "group2", "density"), lty=c(1,2, 1),
bty = "n", angle = c(0, 45, 0), density = c(100, 30, 100),
fill=c("black", "gray", "firebrick"))
Edited2:根据评论的例子
legend("topright", c("group1", "group2"),
bty = "n", angle = c(0, 45), density = c(100, 30),
fill=c("black", "gray"))
legend(2.7, 28, "density", lty = 1, bty = "n", lwd=2, col = "firebrick")