Sunday, June 26, 2022

[FIXED] How to export a plot in PDF with the text and the legend?

Issue

When I try to export a plot in PDF, all the text (titles, axis…) and the legend disappear. Exporting the plot as a PNG works.

I just use the “Export ▾” button in the plot panel.

Is there a way to keep the legend and text on the PDF? An additionnal package maybe?

Thank you and have a good day!


Solution

Here is a simple way to export plots to PDF. You simply use the functions "pdf()" and "dev.off()". It can handle multiple plots and can be used in loops to generate many pages of plots.

#set up the pdf as folder path / name.pdf
pdf("C:/Users/xxx/Desktop/plot1.pdf")

# Generate some data
x<-1:10; y1=x*x; y2=2*y1
plot(x, y1, type="b", pch=19, col="red", xlab="x", ylab="y", main = "A title")
# Add a line
lines(x, y2, pch=18, col="blue", type="b", lty=2)
# Add a legend
legend(1, 95, legend=c("Line 1", "Line 2"),
       col=c("red", "blue"), lty=1:2, cex=0.8)

dev.off() #turn off develop

Plot from http://www.sthda.com/english/wiki/add-legends-to-plots-in-r-software-the-easiest-way



Answered By - Jost
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.