Problems printing .tiff files using ggsave - r

I am trying to print .tiff files and am getting an error here is an example:
ratings <- qplot(rating, data=movies, geom="histogram")
qplot(length, data=movies, geom="histogram")
ggsave(file="length-hist.tiff")
This code produces this error:
Saving 7 x 7 in image
Error in grDevices::tiff(..., width = width, height = height, res = dpi, :
argument "antialias" is missing, with no default
What am I doing wrong? I am using R version 2.15.0 for Mac OS X Leopard and ggplot 0.9.0

With ggplot2 0.9.1 and R version 2.14.1 on Linux, your code works and I can't reproduce this problem. Maybe you should try updating ggplot2?

Related

"plot.new has not been called yet" error in rmarkdown (Rstudio 1.0.44)

I am using a recent version of Rstudio with an iMac
Version 1.0.44 – © 2009-2016 RStudio, Inc. Mozilla/5.0 (Macintosh;
Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko)
And I noticed the notebook function for rmarkdown files. When generating plots, the usual "Plots window" is not used any more, and the plots are generated just below the code chunk.
And I have an error for the following code:
plot(seq(1,10,1))
abline(a=0,b=1)
The error is showed below the code chunk :
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : plot.new has not been called yet
However, when knitting the whole rmarkdown file, there is no error.
So I would like to know how to avoid the error:
by using another code
by using the "Plots window"
or another way.
The following will work
{plot(seq(1,10,1))
abline(a=0,b=1)}
In RStudio, there's a setting in Preferences -> R Markdown to "Show output inline for all R Markdown documents". To get rid of the error, make sure this is unchecked.
This works too
plot(seq(1,10,1))+
abline(a=0,b=1)
In jupyter with R kernel, you will see that error if run the code line by line, just as XR SC mentioned.
This was happening to me because I was adding an invalid parameter to my plot. In my case I was trying to execute:
ggplot(df, aes(x=sales)) + geom_histogram() + title('Plot Title')
And should have been executing:
ggplot(df, aes(x=sales)) + geom_histogram() + ggtitle('Plot Title')
Notice that you must use ggtitle, not title.

Color error in ggplot2 (Error in grDevices::col2rgb(colour, TRUE) : invalid RGB specification)

If I run:
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
bp<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
bp
I see the error: Error in grDevices::col2rgb(colour, TRUE) : invalid RGB specification.
If I run the identical code on my other computer, I get the expected plot. I am guessing that some plotting parameter is the culprit, but I have no idea how to find it. What's going wrong?
The computer that behaves strangely is Mac OS X 10_11_3 running R 3.2.2 via RStudio 0.99.489
Sorry I am late, but the problem is that the function alpha is masked form ggplot2, that's why if you restart R and happen to call alpha after sourcing psych but before ggplot2 it works.
Anyway, the way to solve it is to make explicit that we want the psych function:
reliability = psych::alpha(df)
Restarting R was sufficient to fix the weird behavior. Obviously should have tried that before posting...

How to display Markov Chain plots in Rstudio (markovchain package)

Latest Rstudio and latest R ( in Ubuntu Linux 12.04 - 32bit ).
Most plots will display OK in my Rstudio [Plots] pane.
But some Markov Chain plots throw an Error and don't display.
Here's a very simple example code:
library(markovchain) #loading the package
myMatr<-matrix(c(0,.2,.8,.1,.8,.1,.3,0,.7),byrow=TRUE,nrow = 3)
## defining a transition matrix
rownames(myMatr)<-colnames(myMatr)<-c("a","b","c")
myMc<-as(myMatr, "markovchain")
plot(myMc) # <== Error message starts after this...
gives this Rstudio Console Error:
(and no plot is displayed in Rstudio)
"Error in cut.default(x, binsToUse, labels = FALSE, include.lowest = TRUE, : 'x' must be numeric"
Yet, exactly the same R commands (as above)
work fine in "stand-alone" R 3.2.3! (outside of Rstudio).
And the Markov Chain plot is displayed ok in a new R-window...
Q:
- Is there an easy way/command to tell Rstudio
that I want certain plots
to open in a new R window (just like in pure R),
and NOT in the Rstudio Plot pane?
- Or some other easy solution?

R not producing a figure in jupyter (IPython notebook)

I am very excited about using python and R together and tried using R in Jupyter (ipython notebbok), however, I could not generate figures in the R kernel.
The error message shown below is displayed when I try to plot figures. I highly appreciate your help.
I am using Anaconda and windows 8.
x<-1:10
plot(x)
Error in png(tf, width, height, "in", pointsize, bg, res, type = "cairo", : unable to load winCairo.dll: was it built?
Error in jpeg(tf, width, height, "in", pointsize, quality, bg, res, type = "cairo", : unable to load winCairo.dll: was it built?
The following is displayed in the Anaconda command prompt:
Unable to load 'C:/Users/aa/Anaconda/R/library/grDevices/libs/x64/winCairo.dll'
I found a solution from the post on this group.
Solution I did:
I had the most release of R (R 3.2.0) and following the discussion in the above link, I installed R 3.1.3 and copied winCairo.dll from C:\Program Files\R\R-3.1.3\library\grDevices\libs\x64 to C:\Anaconda\R\library\grDevices\libs\x64.
Copying winCairo.dll from R 3.2.0 does not work for my case.
With this, it plots a figure but with an error message that says "Error in replayPlot(obj): invalid graphics state"
Then, thanks to this link,
I added 'options(jupyter.plot_mimetypes = 'image/png')' and it works fine.
options(jupyter.plot_mimetypes = 'image/png')
x=1:10
plot(x)

RStudio-only error with tcl/tk?

When I compute an ROC plot with the package pROC, I get the following error in RStudio v. 99.9.9:
## Loading required package: tcltk
## Warning: couldn't connect to display ":0"
## Error: [tcl] invalid command name "toplevel".
I thought this was an issue in my OS (Debian with r-base 3.1 installed from APT depending on tcl/tk lib version 8.5), but when evaluating the same code through the terminal, I get the plot rendered without the same error.
Here is a simple example following some pROC example code that will generate the error when knitted in RStudio, but works in vanilla R:
```{r markdown.test}
library(pROC)
features <- runif(25)
labels <- as.factor(runif(25) < 0.75)
lda.model <- lda(as.matrix(features),labels)
lda.pred <- predict(lda.model,as.matrix(features))
lda.roc <- plot.roc(lda.pred$posterior[,1],
c(labels),
ci=TRUE, of="se", #specificities = seq(0,100,5),
ci.type="shape",
ci.col="#1c61b6AA")
```
The error occurs when the confidence interval parameters are added to the call.
Is there something missing from my RStudio install/config, or is this a bug?
Blast from the past! I see that same error today on an instance of RStudio installed in the IBM computing environment called CP4D. Did you ever find an answer?
I think the way to fix on your Macintosh is to install Quartz and let it serve as the X11 server to display output from programs like tcltk or maybe even hist() or quartz() devices. (my team wrote install instructions https://pj.freefaculty.org/guides/crmda_guides/47.mac_R_setup).
I'm not sure what I'll do on the CP4D thing, since it is running inside a Web browser.

Resources