R: Can I fix my legend when saving to PDF?

When i run this code

plot(c(0,1), c(0, 1), type = "n") legend("topleft", legend = c("Model", "Data"), lwd = c(3, NA), pch = c(NA, 16), bty = "n", inset = 0.02, cex = 2) 

as expected, I see the following: Nice legend

However, when I wrap it in pdf("legendTest.pdf") ... dev.off() , the saved PDF has a line through the dot.

extra line

Can this be fixed, or do I just need to save it in a different format? I am on a Mac and the problem is viewing the PDF using Preview and Chrome. I downloaded Adobe Reader to see what it did, and then the legend text and pch dot disappeared, leaving only the model line.

+4
source share
1 answer

You can fix this by explicitly specifying the line type ( lty argument), for example:

 plot(c(0,1), c(0, 1), type = "n") legend("topleft", legend = c("Model", "Data"), lwd = c(3, NA), lty = c(1, 0), # 0=blank, 1=solid (default). See ?par for more. pch = c(NA, 16), bty = "n", inset = 0.02, cex = 2) 

EDIT

The documentation for the lwd argument, in ?par explains that:

'lwd' Line width, positive number, default is '1'. the interpretation is device-specific, and some devices do not use line widths less than one.

The default Mac graphics device R should be one of them. (FWIW, with your code, my own Windows graphics device shows a line through the data point, as in pdf.)

In any case, it is generally safer to use lty than lwd to manage this particular part.

+5
source

Source: https://habr.com/ru/post/1382589/


All Articles