I use ggsave to save the figure generated using ggplot2, this is how I do
figure1<-last_plot()
ggsave(figure1,file="/home/user1/figure1.png",width=15,height=3)
These two lines of code successfully save the figure as png file.
However, when I tried to save it as pdf file,
ggsave(figure1,file="/home/user1/figure1.pdf",width=15,height=36)
The saved pdf file is just a blank page. What's the problem? Thanks a lot.
library(ggplot2)
ggplot(data=mtcars, aes(x=cyl, y=hp))+
geom_point()
figure1<-last_plot()
ggsave(figure1,file="figure1.png",width=15,height=3)
ggsave(figure1,file="figure1.pdf",width=15,height=3)
# works with R 2.15
You can try to save it using pdf() command:
library(ggplot2)
pdf(file="figure.pdf",width=15,height=3)
figure <- ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
print(figure)
dev.off()
I supose that it also doesn't work.
Solution (workaround) that should work is:
1) save it to svg file
ggplot(data=mtcars, aes(x=cyl, y=hp)) + geom_point()
ggsave("figure.svg")
2) then convert it to pdf. E.g. in Linux
rsvg-convert -f pdf -o figure.pdf figure.svg
Related
I am trying to export some unicode characters in the U+1xxxx format from R to pdf using ggplot2 (this is one example: https://unicode-table.com/en/1F321/). In R, the output is great when I have it in the ggtitle as ggtitle("Temperature range \U1F321"):
However, when I export the plot to pdf it appears as these blocks below:
I have tried many things that other people suggested (such as using cairo pdf and the showtext package) but none of it worked. I am using Windows.
Thank you very much!
EDIT: I'm saving as pdf using pdf("example.pdf") or cairo_pdf("example.pdf")
Reproducible code:
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321")
cairo_pdf("plot_cairo.pdf")
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321")
dev.off()
pdf("plot_normal.pdf")
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point() + ggtitle("Temperature range \U1F321")
dev.off()
Also, just want to state my intentions more clearly. I'm just aiming to have a high-resolution ggplot exported from R to Word. When I export it as an image, the characters show well but the resolution is of poor quality. Following suggestions from other users, I'm therefore first exporting it as pdf and then importing it to Word as an object from the "Insert" tab:
Using this method, the plot is of much better resolution in Word, but the unicode characters don't show up well. So if maybe someone else has another suggestion on how to get a high quality ggplot exported from R to Word while still preserving the unicode characters, that would also work! Thank you!!
My plots tab doesn't work - so I am trying to display the output of ggplot in the Viewer tab.
This has been accomplished for printing HTML tables: Force rstudio to use browser instead of viewer. Can this be done for plots?
library(ggplot2)
gg.plot <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
Is there a method to print to viewer?
I posted this as a comment, but couldn't get the formatting right, so I'm making it an answer. This code will create a new window to put your plot in.
library(ggplot2)
data(iris)
x11() # creates the new window
ggplot(data = iris, aes(Petal.Length)) + geom_histogram()
dev.off() # closes the window when you're done with it
Unfortunately, I don't think this is what the viewer pane was designed for. This article from Ian Pylvainen suggests that this pane is exclusively for web content. Unless you were attempting to visualise your plots in an HTML instance (produced from Markdown, Shiny, htmlwidgets, etc.), I would not recommend Rstudio's Viewer as a viable solution.
To continue with #Joseph Clark John's recommendation, using the variety of other devices R offers might be of use to you. Their recommendation to use x11() is specific to linux distributions so if using Windows or MacOs, you could produce another window using the windows() or quartz() commands, respectively (See documentation for all relevant devices).
This post touches on this:
Make R Studio plots only show up in new window
Alternatively, you could use plotly::ggplotly() to display a ggplot in the Viewer pane:
library(ggplot2)
library(plotly)
data(iris)
plotObj <- ggplot(data = iris, aes(Petal.Length)) + geom_histogram()
plotly::ggplotly(plotObj)
I am creating a lot of facet plots with ggplot2 and writing those into a multiple page pdf file. I added some sample data as an example.
reps <- 1000
df <- data.frame(id=rep(letters[1:25], each=reps),
group=rep(LETTERS[1:25], each=reps),
x=runif(reps*25),
y=runif(reps*25))
pdf('test.pdf', width=11.69, height=8.27)
for(i in seq(4)) {
p <- ggplot(df, aes(x,y, group=group)) +
geom_line() + geom_point() +
facet_wrap(~ id)
print(p)
}
dev.off()
To view the PDF it is really nice, but most of the time these big files with vector graphics kill my print jobs.
Is there a way to handle this properly? For example, creating PNG images and saving them in multipage PDFs? Or, am I the only one having problems printing such files?
I am aware that the PNG file might be bigger, but I guess the printer can handle it much better than a vector graphic.
The only solution I found so far is the following: https://helpx.adobe.com/acrobat/kb/quick-fix-print-pdf-image.html
I think this might be the best way since the creation of PDF is the same, you just switch to "print as image" in the advance printing dialog in Acrobat Reader...
I am preparing a presentation in Japanese and would like the titles and legend names of my images to be in Japanese. I can get the text to render just fine in RStudio but when the image is rendered the Japanese characters just appear as boxes.
x=-10:10
y=x*x
df=data.frame(x,y)
ggplot(df, aes(x,y)) + geom_line() + ggtitle("テスト")
Thank you.
It seems that you do something like this. I am using Mac, and I initially did not see the letters you specified in ggplot graph. But the following code is printing the letters.
theme_set(theme_gray(base_size=12, base_family="HiraKakuProN-W3"))
ggplot(df, aes(x,y)) +
geom_line() +
ggtitle("テスト")
Having an unusual problem with creating multiple files in R with ggplot2.
I've got multiple plots to create for multiple people, so I'm creating all the plots for each person in a pdf. So it goes something like this...
for(i in 1:10)
{
pdf(paste("person",i,".pdf",sep=""))
ggplot2(...)+.........
ggplot2(...)+.........
ggplot2(...)+.........
ggplot2(...)+.........
dev.off()
}
I've verified that all the code to create the plots is working and that creating a single pdf works, no problems there. The problem arises when I try to run the loop, it creates the files, but they're blank. I've tried everything I can think of and can't seem to find any information about this. I've tried in RStudio (Windows) and command line (ubuntu), both create the same issue.
Any insight or an alternative would be appreciated, thanks
You need to use print for each plot want you output into a pdf.
library(ggplot2)
dat = data.frame(x1=rnorm(10), x2=rnorm(10))
for(i in 1:2){
pdf(paste("person",i,".pdf",sep=""))
p1 = ggplot(dat, aes(x=x1)) + geom_histogram()
p2 = ggplot(dat, aes(x=x2)) + geom_histogram()
print(p1)
print(p2)
dev.off()
}