pscl package of R - r

I was running the xyplot of presidential elections in the pscl package and got the error
data(presidentialElections)
library(lattice)
xyplot(demVote ~ year | state,
panel=panel.lines,
ylab="Democratic Vote for President (percent)",
xlab="Year",
data=presidentialElections,
scales=list(y=list(cex=.6),x=list(cex=.35)),
strip=strip.custom(par.strip.text=list(cex=.6)))
Error:
Error in recordGraphics(drawGrob(x), list(x = x), getNamespace("grid")) :
invalid graphics state
I am a new user to R and would appreciate if anyone can help me to fix the error.

Try dev.off() and then re-run the same code.

I personally use tinn r to program in R and find it very useful, you can close/open R easily and still send command lines in. (you can even DL different versions of R to load for different packages)

Related

BiodiversityR::accumcomp.long function not found

I am trying to use the function accumcomp.long in the Biodiversity.R package in order to adjust my species accumulation curves in ggplot.
However, calling the function accumcomp.long gives me an error, suggesting that this function perhaps does no longer exists.
accum.long1 <- accumcomp.long(Accum.1, ci=NA, label.freq=5)
Error in accumcomp.long(Accum.1, ci = NA, label.freq = 5) :
could not find function "accumcomp.long"
The function BiodiversityR::accumcomp.long
returns
Error: 'accumcomp.long' is not an exported object from 'namespace:BiodiversityR'
I am not certain whether this is an issue with the package, the way the package is loaded into my R space, or perhaps unrelated to the package simply the format of my data.
My community and environment files are of dataframe class. I have tried loading them as .csv files as well as .rda files, but the error remains.
??accumcomp.long
renders "No results found".
I am using R version 3.5.3 (2019-03-11)
Any suggestions as to what else I could try?
Thanks in advance!
I re-installed the package BiodiversityR and included force = T (as per this thread https://community.rstudio.com/t/could-not-find-function-error-even-though-fun-is-in-namespace-and-has-an-rd-file/91243). I then reloaded the package. I am not sure what force = T does, but the accumcomp.long function now works.

ggplot2 error: Error in default + theme : non-numeric argument to binary operator

I have been getting the error Error in default + theme : non-numeric argument to binary operator. I have been using R and teaching R for a long time but I can't find this problem. I have included a reproducible example that fails this way below:
library(tibble)
library(ggplot2)
brains <- as_tibble(brains)
brains <- brains[1:10, ]
brains
ggplot(brains, aes(x = BodyWt, y = BrainWt)) +
geom_point()
The error occurs when executing the ggplot() statement.
My hardware is an HP Laptop 15-ef0xxx. I am running Windows 10 Home version 2004. I am running RStudio community edition "Water Lily" and R version R x64** 4.0.2.
I know this is a simple error and it is driving me crazy.
So I finally solved this problem. On the github issue I had opened Hiroaki commented that "One possibility is that you might set a invalid default theme in your .Rprofile, but I'm not sure..." (see link to issue below).
I'm not sure if your R file is part of a project but mine is.
So I went back and deleted the theme_set() line in my R file, went in and double checked all my project options and selected the option "Disable .Rprofile execution on session start/resume" and "Quit child processes on exit". And then I restarted the R session and now everything works. Including on the default R editor console.
I'm not sure if all those steps are necessary but that seemed to do the trick for me! Hope it helps.
I thought this was an RStudio issue but it seems it's possibly a ggplot2 > problem. I have verified using two different datasets that the same >error comes up when I try using ggplot2 in RStudio or using the default R >console. I get the exact same error with code that's been working fine >but now suddenly won't. I have opened an issue on Github (ggplot2) with a >reprex. Might be worth checking there: >https://github.com/tidyverse/ggplot2/issues/4177
I know this is not an answer per se but I don't have enough reputation >points to add a comment to the previous answer but I thought linking to >the issue on Github might help.
I think you have numbers quoted somewhere and you are trying to perform mathematical operation on character values. Consider
x <- c("5","6")
y <- x/1
> y <- x/1
Error in x/1 : non-numeric argument to binary operator
Now try converting x to numeric and perform the same operation.
y <- as.numeric(x)/1
> y
[1] 5 6
So, you need to use as.numeric on your variable.
The following should resove this issue
ggplot(brains, aes(x = as.numeric(BodyWt), y = as.numeric(BrainWt)))

how do I create a probability plot in R using R-studio

I want to create a lognormal (or other distribution) probability plot in R (for R-studio). I have looked around on the web for an example but none of the examples tell me what package I need to install in order to use the function.
logn_prob_plot <- function()
{
x<-rlnorm(10,5,1)
x
probplot(x,qdist=qlnorm,xlab="failure time",ylab="lognormal probability")
}
Error in probplot(x, qdist = qlnorm, xlab = "failure time", ylab = "lognormal probability") : could not find function "probplot"
Writing up the comment thread as an answer:
The error (could not find function "probplot") is showing up because a necessary package is not installed. It's not specifically related to creating a probability plot.
Googling "r probplot" turns up the documentation for the package e1071, which is available in CRAN.
The package can be installed by entering install.packages("e1071") in your terminal or by selecting Tools -> Install Packages in the RStudio GUI. You can then load that package using library("e1071").

Error in clusplot function

I am playing with cluster analysis in R and I would like to plot the cluster:
teste<-data.frame(v1=rnorm(100),v2=rexp(100),v3=runif(100) )
fit.c<- kmeans(teste,2)
aggregate(teste,by=list(fit.c$cluster),FUN=mean)
clusplot(teste, fit.c$cluster, color=TRUE, shade=TRUE,labels=2, lines=0)
But it gives me this error:
Error in clusplot.default(teste, fit.c$cluster, color = TRUE, shade = TRUE, :
4 arguments passed to .Internal(nchar) which requires 3
Does anyone know what is happening?
Thank you
I ran your code and got no problems. I am guessing that there is a version mismatch between your R and the packages you are using. Can you make sure that your cluster package is up to date (and any other packages you are using for that matter), and/or update to the latest version of R?
If that doesn't work, can you add the sessionInfo() information? That is often helpful for diagnosing these sorts of problems.

Incompatibility between ggplot2 and other packages in R?

I am revising a paper for submission and would like to replace the old lattice graphics with shiny new ggplot2 versions. However, I run into compatibility problems between ggplot2 and two packages that are absolutely crucial for my analyses, coin and arm. When executing the following example from the manual
qplot(sleep_rem / sleep_total, awake, data = msleep)
I get an error message saying:
Error in function (classes, fdef, mtable) :
unable to find an inherited method for function "empty", for signature "data.frame"
as soon as either coin or arm are loaded.
Here are the details:
Running R 2.10.1, empty .RData file
require(ggplot2)
require(xtable)
require(MASS)
require(gdata)
require(car)
require(Hmisc)
require(psych)
qplot(sleep_rem / sleep_total, awake, data = msleep)
require(coin)
qplot(sleep_rem / sleep_total, awake, data = msleep)
require(arm)
qplot(sleep_rem / sleep_total, awake, data = msleep)
Is this reproducible with R 2.12? If not, might it be worth upgrading? I must confess I am loath to update a working system, especially when on a tight deadline.
It's only the coin package that seems incompatible. Reason is that one of the packages loaded together with coin (modeltools to be exact) also contains a function empty() that masks the function empty() of plyr. Updating to R 2.12 won't change a darn thing, as both packages are contributed by other authors. You should mail the author of the ggplot package and tell him to specify plyr::empty() in his code. Or even the guys from the plyr package, as the problem might actually be there.
A possible hack could be :
require(coin)
empty <- plyr::empty
qplot(sleep_rem / sleep_total, awake, data = msleep)
rm(empty)
This works on 2.11.1 using following data frame:
msleep <- data.frame(
sleep_rem=c(1,2,3,4,5),
sleep_total=c(10,20,30,40,50),
awake=c(5,4,3,2,1)
)
Make sure you remove empty from the workspace right after plotting. Otherwise you might get trouble with the other packages that are loaded. You'll have to do that with every plot you make after you loaded the package coin.

Resources