Points3D Function in R - r

I have the scatterplot3d package installed in R. When I load it with library(scatterplot3d) or require(scatterplot3d) I am able to create a 3d scatter plot. However, when I try to use the points3d function I get the following error:
Error: could not find function "points3d"
I tried reinstalling the package to no avail (both inside R and as a tarball using R CMD INSTALL in the command line). I am running Xubuntu 12.10 kernel 3.8.7-030807-generic and R version 2.15.3 (2013-03-01).
Entering locate points3d in the command line gave me no results.
I also tried the par.mar default settings command as explained in the manual.

scatterplot3d does an interesting object-oriented twist on the usual R practice. The object returned from the function call includes the points3d function as built-in part of the object but it is not in the Global environment. It is intended that you add to the existing plot-object using that "attached" function that is not a free-living organism but rather a domesticated animal that only exists in the object corral, so you would use this as your syntax:
object$point3d(x,y,z)
I do "feel your pain" but can show you how to overcome the frustration, since I created a working example yesterday: Using scatterplot3d to plot a sphere

You need to intall the package plot3D in the usual way via
install.packages("plot3D")
Then you just need to import, generate the dataset and use the function points3D()
library(plot3D)
x = rnorm(100)
y = rnorm(100)
z = x + y + rnorm(100,0,1)
points3D(x, y, z, col = rainbow(1000))
This is the plot generated by the code above

Related

ggpubr can't find 'mean_se' unless ggpubr is attached via library()

Summary of problem: When I try to add summary stats to a ggpubr plot via the "add" parameter, ggpubr can't find the summary stat functions (example code below). For instance, if I am trying to add error bars with add="mean_se" I get an error message and no error bars.
Unsatisfactory solution: Attaching ggpubr by calling library(ggpubr) would fix this problem. See this answer.
Why the above solution is unsatisfactory: I am developing a package, and so would like to avoid attaching other packages via calls to library() - my understanding is that this is best practice, to avoid polluting the namespace with things the user might not have anticipated would get loaded.
MY QUESTION: Is there some way to get ggpubr to find mean_se without attaching the package?
Example code (in an .R file in my package):
make.plot = function(){
utils::data("iris")
ggpubr::ggbarplot(
data = iris,
x = "Species",
y = "Sepal.Length",
add = "mean_se")
}
Example output:
> devtools::load_all(".")
# i Loading MyPackage
> make.plot()
# Warning message:
# Computation failed in `stat_summary()`:
# object 'mean_se_' of mode 'function' was not found
One thing that should work, but doesn't, is passing "ggpubr::mean_se_" as the add argument. This avoids the error message, but produces an incorrect plot. The plot should look like this:
But passing "ggpubr::mean_se_" instead produces:
Additional weirdness: If I ever add a call to load ggpubr, build MyPackage with devtools::load_all("."), and run it, then the above code never fails until I quit and reload RStudio, even if I delete the call library(ggpubr) from my package and build it again.
Since you're creating a package you can ensure that mean_se_ is in the search path by importing it in your function.
If you use roxygen you can add the tag #importFrom ggpubr mean_se_:
#' #importFrom ggpubr mean_se_
make.plot = function(){
utils::data("iris")
ggpubr::ggbarplot(
data = iris,
x = "Species",
y = "Sepal.Length",
add = "mean_se")
}
Then you run devtools::document() and run your function, and your output should look like this:
If you look at the way the add parameter is handled inside ggpubr, it is actually matched as a string to one of the summary functions. It seems that the summary functions need to be on the search path when ggbarplot is called.
The easiest way round this is to copy the function over to your own package namespace:
mean_se_ <- ggpubr::mean_se_
make.plot = function(){
utils::data("iris")
ggpubr::ggbarplot(
data = iris,
x = "Species",
y = "Sepal.Length",
add = "mean_se_")
}
make.plot()
Created on 2022-05-04 by the reprex package (v2.0.1)

R creates empty pdf from ggplot graphic when I source script but not when I run it [duplicate]

Let's assume I have 2 source files, the first one named example1.r and the second one example2.r (given below).
example1.r
plot(1:10,1:10)
example2.r
qplot(1:10,1:10)
When I source example1.r, the graph is drawn. It does not, however, when I source example2.r. What is the solution here?
(qplot in example2.r is ggplot2's function)
Update:
.R files: source's option print.eval=TRUE will lead to printing behaviour of the evaluation result like in the interactive command line.
source("Script.R", print.eval=TRUE)
.Rnw files: knitr by default emulates the behaviour of the interactive command line wrt. printing. Note that knitr can be specified as Sweaving engine also for R package vignettes.
This is my original answer. But note that this workaround is IMHO completely obsolete now (and it always was good for a small lazy niche only).
This is the famous FAQ 7.22: Why do lattice/trellis graphics not work?.
For grid graphics like ggplot2 or lattice, you need to print the graphics object in order to actually draw it.
Interactively on the command line this is done automatically. Everywhere else (inside files to be sourced, loops, functions, Sweave chunks) you need to print it explicitly.
print (qplot (1 : 10, 1 : 10))
Alternatively, you can redefine qplot to do the printing:
qplot <- function (x, y = NULL, z = NULL, ...) {
p <- ggplot2::qplot (x = x, y = y, z = z, ...)
print (p)
}
(this changes the axis labels to x and y).
I use this approach in vignettes where I want to write code exactly as a user in an interactive session would type it.

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").

R programming Graph plot not appearing ggplot2 [duplicate]

Let's assume I have 2 source files, the first one named example1.r and the second one example2.r (given below).
example1.r
plot(1:10,1:10)
example2.r
qplot(1:10,1:10)
When I source example1.r, the graph is drawn. It does not, however, when I source example2.r. What is the solution here?
(qplot in example2.r is ggplot2's function)
Update:
.R files: source's option print.eval=TRUE will lead to printing behaviour of the evaluation result like in the interactive command line.
source("Script.R", print.eval=TRUE)
.Rnw files: knitr by default emulates the behaviour of the interactive command line wrt. printing. Note that knitr can be specified as Sweaving engine also for R package vignettes.
This is my original answer. But note that this workaround is IMHO completely obsolete now (and it always was good for a small lazy niche only).
This is the famous FAQ 7.22: Why do lattice/trellis graphics not work?.
For grid graphics like ggplot2 or lattice, you need to print the graphics object in order to actually draw it.
Interactively on the command line this is done automatically. Everywhere else (inside files to be sourced, loops, functions, Sweave chunks) you need to print it explicitly.
print (qplot (1 : 10, 1 : 10))
Alternatively, you can redefine qplot to do the printing:
qplot <- function (x, y = NULL, z = NULL, ...) {
p <- ggplot2::qplot (x = x, y = y, z = z, ...)
print (p)
}
(this changes the axis labels to x and y).
I use this approach in vignettes where I want to write code exactly as a user in an interactive session would type it.

pscl package of 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)

Resources