The following code is supposed to export the following graph (with a legend outside of the graph) to pdf. But the legend is not showing up in the resulting pdf. However, if I only run the code without the pdf line the legend shows up in the plot viewer in 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))
. Position your legend using x and y coordinates.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.