图表外的R图例未以pdf显示

时间:2017-11-26 08:38:08

标签: r pdf plot graph legend

以下代码应该将以下图表(图表外的图例)导出为pdf。但是传说中的图例并没有出现在pdf中。但是,如果我只运行没有pdf行的代码,则图例会显示在Rstudio中的绘图查看器中。

pdf(paste("testgraph.pdf", sep=''), paper="a4r", width=10, height=10)

set.seed(1) # just to get the same random numbers

plot(1:30, rnorm(30), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')

legend("topright", inset=c(-0.3,0),c("group A", "group B"), pch = c(1,2), lty = c(1,2))

dev.off()

2 个答案:

答案 0 :(得分:0)

这段代码对你有用吗?

pdf(paste("testgraph.pdf", sep=''), paper="a4r", width=10, height=10)
set.seed(1) # just to get the same random numbers
plot(1:30, rnorm(30), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')
legend("topright",c("group A", "group B"), pch = c(1,2),lty = c(1,2))
dev.off()

答案 1 :(得分:0)

xpd = TRUE中设置legend(),并使用par(mar = c(c(bottom, left, top, right) + 0.1))获得更大的边距。使用x和y坐标定位图例。

pdf("testgraph.pdf", paper="a4r", width=10, height=10)

par(mar = c(c(5, 4, 4, 6) + 0.1))
set.seed(1) # just to get the same random numbers
plot(1:30, rnorm(30), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')

legend(x = 31, y = 1, legend = c("group A", "group B"),
       pch = c(1,2), lty = c(1,2), xpd = TRUE)

dev.off()