PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, June 26, 2022

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

 June 26, 2022     export, graph, plot, r, save     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing