This question already has an answer here:
Call to plot doesn't actually produce plot
(1 answer)
Closed 9 years ago.
I am generating a plot which I am able to see in the RMarkdown output but whenever I am trying to save it, I am getting just a blank(white) image. I am just adding following two lines before and after plot
png("Output.png")
#Plot code
dev.off()
It was working. And suddenly it stopped working. Can someone help me on this??
edit
When I do it with pdf
pdf("output.pdf")
#plot code
dev.off()
I am getting error as:
There was error opening this document. This document cannot be opened because it has no pages.
Thanks.
From the comments I gather that I was right, you need to print the resulting ggplot2 object in order to show the plot, see also R FAQ 7.22.
Related
I've found a couple of similar posts but unfortunately not one reply seems to fix my problem.
Data = working on the uscrime dataset.
Essentially, I'm having trouble my plots when using par() in RStudio. It's not the first time it happens that all of the sudden after re-rerunning a chunk in R, the plot is no longer displayed inline nor in the Plots window.
My code is the following:
par(mfrow=c(3,5))
for(i in colnames(crime[,1:15])){
plot(crime[,i],crime[,16], main=print(paste("Crime vs", i)), xlab=i, ylab="Crime")
lines(lowess(crime[,i],crime[,16]), col="red")
abline(lm(crime[,16]~crime[,i]), col="blue")
}
The first time I ran the chunk the plots showed up:
Now, as soon as I tried to plot something different (same data but with a transformed column) using the same approach, nothing appeared inline. Here's my code:
par(mfrow=c(3,5))
for(i in colnames(crime2[,1:15])){
plot(crime2[,i],crime2[,16], main=print(paste("Crime vs", i)), xlab=i, ylab="Crime")
lines(lowess(crime2[,i],crime2[,16]), col="red")
abline(lm(crime2[,16]~crime2[,i]), col="blue")
}
I tried restarting R and running everything again and ow I'm getting a weird result where some of the information I passed to my first par() call is getting displayed in my charts (see how I have two blue curves when I should have a blue and a red one).
Now, I've been reading about 'devices' and plots but since I'm an R noob I can't figure out what's wrong on my own. It looks like I have to call dev.off() at some point but I don't quite get it. My expectation is that there's something wrong (or an additional step to take) with my par() calls but I haven't found the replies in R Documentation.
Here's a view of my Markdown config
Hi guys,
I have set up a new conda R-environment (on my ubuntu 20.04) and oddly, the output of the base plot function does not show symbols on the axes or axis labels but little squares with numbers instead (see attached picture).
When I use the plot function in an R-Markdown code chunk and export it as PDF, the plot is displayed correctly. However, in RStudio or when exporting the Rmd to html, the symbols appear like in the picture.
I realize this could have to do with the encoding, but don't know how to solve it. Has anyone encountered this problem before or can give me advice how to troubleshoot it?
Many thanks in advance!
Simply adding the package r-cairodevice to my R environment solved the problem.
I'm trying to, within a for loop, create and save multiple plots from fb prophet and save them to jpeg files.
Despite following the code for many questions related to saving R plots in a loop on stackoverflow, none of the solutions seem to be working and I can't figure out what I'm doing wrong. The below code would be within a for loop:
jpeg(filename="plot1.jpeg")
plot(model, future_forecast) + ggtitle(Material_name)
dev.off()
#now plot seasonalities
jpeg(filename="plot2.jpeg")
prophet_plot_components(model, future_forecast)
dev.off()
I expect that this code would create two separate plots using the prophet plotting functionality, and save them to jpeg files. What actually happens is that the program saves one plot file correctly, and a second plot file as a blank plot.
When you execute (or source) a script as opposed to running it line-by-line, some of the output is suppressed. This is what is happening here. In order to force the output of your plot, just enclose the statement within the print function.
For example:
print(prophet_plot_components(model, future_forecast))
This question already has answers here:
How to save a plot as image on the disk?
(11 answers)
Closed 5 years ago.
Most of my plots are made with ggplot2 and the ggsave command saves them where they should be with one line. However, mosaic plots using the vcd package are best for my data.
Problem: I don't get an error message with the following code. R says that it has saved my plot, but the plot that gets saved is the last ggplot plot I created, not the mosaic plot I want. Of course I can manually save in RStudio, but I'm quite sure there is a better way. Any ideas?
Onlyaround <- subset(prepData, preposition=="around")
attach(Onlyaround)
mytable <- table(exp_group, session, result)
ftable(mytable)
mosaic(mytable, shade=TRUE, legend=TRUE, main = "Around by Group")
margin.table(mytable)
ggsave("pics/around_mosaic.png")
detach(Onlyaround)
ggsave() is in fact the command to save ggplots, so it's no surprise that it doesn't save you mosaic plot. The standard 'R' way to save plots will work just fine:
jpeg("pics/around_mosaic.png")
mosaic(mytable, shade=TRUE, legend=TRUE, main = "Around by Group")
dev.off()
This question already has answers here:
Plot size and resolution with R markdown, knitr, pandoc, beamer
(2 answers)
Closed 8 years ago.
I'm starting working with R Markdown and I'm not understanding how to generate bigger plots. With today screens the plots can be much bigger.
How do I control the plots width/heigh in a Markdown report?
Thanks for the help.
See this SO answer for how to specify these details in chunks.
To set global chunk options, use opts_chunk$set(out.width='750px', dpi=200) inside your first chunk. See this page on chunk options for more.