简体   繁体   English

图表外的 R 图例未显示在 pdf 中

[英]R legend outside of graph not showing in pdf

The following code is supposed to export the following graph (with a legend outside of the graph) to pdf.以下代码应该将以下图表(图表外有图例)导出为 pdf。 But the legend is not showing up in the resulting pdf.但是图例没有出现在生成的 pdf 中。 However, if I only run the code without the pdf line the legend shows up in the plot viewer in Rstudio.但是,如果我只运行没有 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()

Is this code helpful for you?这段代码对你有帮助吗?

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()

Set xpd = TRUE in legend() and get wider margins using par(mar = c(c(bottom, left, top, right) + 0.1)) .legend()设置xpd = TRUE并使用par(mar = c(c(bottom, left, top, right) + 0.1))获得更宽的边距。 Position your legend using x and y coordinates.使用 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM