After keras fit is called, although with interactive metrics turned off by argument, I can not output plots to knitr generated HTML anymore.
I've tried dev.off() after keras fit call with and without plot.new(), as well as various combinations of dev.flush(), dev.new() etc.
I want calls to ggplot() produce plots, but instead I get errors such as replayPlot(x): invalid graphics state without running dev.off() after keras fit, and just no plot rendered in html output if I run dev.off() & plot.new().
Related
Using the forest.robu function from the robumeta package. Trying pdf(filename="test.pdf",width=20,height=8) suggested here did result in the error "unused argument filename". Any idea how I could make a plot from this function that does not fit the plot window fit in an output file? Thank you!
I'm guessing that you don't yet understand how R graphics devices are used. One needs to set a particular output device up and then print to it with one of the three graphics paradigms and then execute dev.off() to finish the process. Failing to execute dev.off() with ps or pdf or png will start a file but then it doesn't get finish and will be unreadable. I'm guessing that forest.robu constructs an object using either ggplot or lattice function calls, so do this:
?pdf; ?Devices; ?pdf.options # the help pages have further useful links
pdf(file= "My_grph.pdf", width=20, height=8)
gobj <- forest.robu( ..... whatever ...)
print(gobj)
dev.off()
If on the other hand, forest.robu() uses base-graphics plotting, then the print call is unneeded and might even cause problems so leave it out.
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
tl;dr
What is the R code that takes the keras history form and produces not the tensorflow/keras plot shown below but the ggplot shown slightly farther below?
Details:
Rstudio has a wrapper for keras:
https://tensorflow.rstudio.com/guide/keras/
When training a model, it displays a plot of the loss and metrics over time. I think it is using plotly. I am not interested in the graph that has these stylings.
You can take the history object an plot it again, and it generates a new plot. If you have 'ggplot2' installed, then it uses ggplot.
I am looking for something like this, the product of ggplot2:
What is the R code that takes the keras history form and produces that ggplot?
I am not looking for the wrapper which is 'plot(history)', but the set of frame manipulations and input commands to ggplot that generate the output. It should contain something of the form'ggplot(history, aes(...)) + ...'
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))
I'm trying to plot a cox proportional hazard model in R. (or a logit model)
I used the following code (which I copied from https://sites.google.com/site/daishizuka/toolkits/plotting-logistic-regression-in-r)
c<-coxph(formula=Surv(year, promo)~prov.yrs, data=cul)
curve(predict(c, data.frame(prov.yrs=x), type="risk"), add=TRUE)
I get the error message
Error in plot.xy(xy.coords(x, y), type = type, ...) :
invalid graphics state
I believe there is something wrong with plotting this, so I was wondering if there is a way to plot this. I get the same error message when I use glm. Any help will be appreciated!!
The example you copied from shows a logistic regression, but you are fitting a coxph model, they are very different in how they are handled.
If you just want a plot of the the hazard ratio then your code will basically work (except you are adding to a plot that is not there, which may be what generates the error, try changing add to FALSE).
If you want to plot the survival curve(s) then use the survfit function to get the predicted survival information and plot that.
The error message suggests you did not have a device open or perhaps there was some other problem with the plot you were trying to add to? That code produces a plot over a range input [0,1] with a toy example I built from the coxph help page. Perhaps your range for the 'prov.yrs' is different than an existing plot, or there is no device open? Try plot.new(), plot whatever else you were going to use, and then rerun? (The add=TRUE will suppress plotting of the box, axes and labels.)