Issue installing packages "labeling" and "scales" in RStudio 2022.02.3 - r

I usually plot my data with the following ggplot syntax:
ggplot(sent.TN.yr.avg, aes(y = (log10(mean)), x = YEAR, color = ECOREGION)) +
geom_point(size = 1, alpha = 0.4) +
geom_smooth(se=TRUE) +
xlab(expression(paste("Year"))) + ylab(expression(paste("Mean TN (", mu, "g", L^-1,")"))) +
theme_light()
But this recently led me to the following loadnamespace(name) error:
Error in loadNamespace(x) : there is no package called ‘labeling’
I'm not sure how this code prompted an error related to "labeling." Still, I tried restarting RStudio and installing that, but it cannot be supported by this version of RStudio (2022.02.3). I also tried the same thing with the "scales" package and got the same result. I've also tried changing the repository to different CRAN mirrors with no success.
Any tips?

Package ggplot2 imports package scales which imports package labeling. So in order to use ggplot you should have installed the other two packages as well.
The version of RStudio most likely has nothing to do with it. But if you recently switched from R version 4.1.* to R4.2.* you have to upgrad all your installed packages (they have to be recompiled after a major R version release).
You can do this in RStudio: Tools -> Check for Package Updates...
or directly in R with: update.packages(checkBuilt = TRUE)
By the way, on the ggplot2 page there is an example of how mathematical expressions can be inserted into axis labels:
https://ggplot2.tidyverse.org/articles/faq-axes.html?q=superscript#how-can-i-add-superscripts-and-subscripts-to-axis-labels

Related

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 messages in R when doing a basic heatmap

Here is the code:
data(attitude)
library(ggplot2)
library(reshape2)
qplot(x=Var1, y=Var2, data=melt(cor(attitude)), fill=value, geom="tile")
Here is the error that appears:
Error in .Call("_scales_doColorRamp", PACKAGE = "scales", colors, x,
alpha, : "_scales_doColorRamp" not available for .Call() for
package "scales"
I already tried to delete and reinstall package "scales" or to restart R but with no change.
EDIT: Problem solved. I desinstalled and reinstalled the package "scales" but, this time, when they asked me "Do you want to install from sources the package which needs compilation?" during the installation, I answer "no". The script works now. Does anyone know why it solved it?

Error there is no package called RevoUtilsMath with R Markdown

I am running R 3.4.3 and 3.5.1 (non-Microsoft version) and RStudio version 1.1.456. I am trying to knit some code into RMarkdown. However, I get the following error:
Error in library(p, character.only = TRUE) : there is no package called 'RevoUtilsMath'Calls: <Anonymous> ... suppressPackageStartupMessages -> withCallingHandlers -> library
Execution halted
The package RevoUtilsMath is part of the MKL install with Microsoft R. I cannot install it as a supplemental package with 'regular R'. The script itself runs fine, it just does not work in R Markdown.
The following libraries are loaded:
```{r loadLibraries, echo=FALSE, warning=FALSE}
library(RODBC)
library(dplyr)
library(markovchain)
library(DT)
library(reshape2)
library(knitr)
library(ggplot2)
library(scales)
library(PerformanceAnalytics)
library(plotly)```
The missing package error happens when executing the code below in Markdown. It is called using this code.
```{r histogram1, echo=FALSE, cache=TRUE}```
The histogram1 code is below (very standard ggplot).
g <- ggplot(dataClean, aes(x = IncSnapshotDay, fill = Represent)) +
geom_histogram(bins=70, alpha = .8) +
scale_fill_manual(values = colors) +
scale_x_continuous(labels = comma, limits = c(0,40000)) +
facet_wrap(~SnapshotDay) +
ylim(0,4000) +
theme_bryan()
g
I thought that maybe one of these packages has a dependency, so I ran the following to find out.
library(tools)
> dependsOnPkgs('RevoUtilsMath')
It returns character(0) which indicates that none of the packages depend on it. I did a test of the function on ggplot2, and it works
dependsOnPkgs('ggplot2')
[1] "dendextend" "GGally" "ggthemes" "plotly" "viridis" "caret" "crosstalk"
[8] "DT"
So why does R Markdown/knitr generate this error since the code itself runs fine outside of Markdown, and how do I fix this?
I think you are experiencing a problem similar to one I just had though, without more detail, it is hard for me to know.
In my case, the problem was caused by knitr caching the list of packages used by a previous author (using Microsoft R). The immediate solution was to simply clear the knitr cache (via the "Knit" drop down menu in RStudio) before attempting to knit the code.
I still don't really understand why this is happening or how to avoid it in future situations, but this at least provides a way to create the document even in the face of this behavior.

Computation failed in `stat_smooth()`: object 'C_crspl' not found

I am trying to add a geom_smooth() to a qplot() with the following code:
library(ggplot2)
library(ggplot2movies)
qplot(votes, rating, data = movies) + geom_smooth()
However, the smoother is missing from the plot. I also receive the following warning message:
Computation failed in stat_smooth(): object 'C_crspl' not found
Does anybody know what is wrong here?
This is my setup:
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.1 LTS
I had similar issues:
# `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
# Warning message:
# Computation failed in `stat_smooth()`:
# object 'C_crspl' not found
and
# Warning message:
# Computation failed in `stat_smooth()`:
# object 'C_magic' not found
when using geom_smooth(method="gam"). The problems vanished when I explicitly loaded the mgcv package version 1.8-17. I guess by default, ggplot (I use 2.2.1.9000) looked at 1.8-16, which was also in the search path. So you might want to update mgcv or make sure that the latest version is used.
I could not reproduce your problem: it works fine in my system. I guess you have a problem with some ggplot2 dependency. Try restarting your session and reinstall ggplot2.
If that doesn't work, test geom_smooth() with a different dataset and a different method, e.g. qplot(Sepal.Length, Sepal.Width, data = iris) + geom_smooth(method = 'lm')
I had the same exact issue. Tried loading mgcv explicitly and this did nothing. Since I originally relied on ggplot2 to install mgcv, I went back and installed mgcv myself so that the newest version was included and then re-installed ggplot2 and the issue went away. This is on Ubuntu 16.04 with ggplot2 2_2.2.1 and mgcv 1.8-23.
I also had this problem and almost gave up. However, when I simply reinstalled mgcv before and ggplot2 later, the script simply went back to working as before.

Error in as.environment(x) : invalid object for 'as.environment' in R

I am a new user of R, i have tried the following code. It gives an error. Can anyone help, please. The codes are:
library(ggplot2)
data(diamonds)
qplot(price, data=diamonds)
I am running R (2.15.3) in a x64 Wintel computer, having the very same problem.
I just googled around and found the following solution:
1st - From a fresh R session (not using RStudio, just in case), uninstall the packages "ggplot2", "scales", "plyr", "reshape2", "gtable"
remove.packages(c("ggplot2","scales","plyr","reshape2","gtable"))
2nd - quit R and go to your library directory. I had found that plyr library is not completely removed by remove.packages, keeping the plyr ddl in the system. Then manually remove the library folders, if needed.
3rd - Start R again and install again the libraries.
install.packages(c("ggplot2", "scales", "plyr", "reshape2", "gtable"))
4th - Update packages to get the latest versions.
update.packages(ask=FALSE, checkBuilt=TRUE)
5th - I runned a very symple example to check that ggplot2 is properly installed:
library(ggplot2)
library(gridExtra)
ggplot(mtc, aes(x = factor(gear))) + geom_bar(stat = "bin")
A very simple barplot should be rendered.
Hope it works for you!
D.

Resources