expression function used in ggpubr::ggarrange - r

This is my code. The object fig was a list with three ggplot outcomes.
mylabel <- expression("PM"[2.5], "SO"[2], "O"[3])
ggpubr::ggarrange(fig[[1]], fig[[2]], fig[[3]], ncol=1, nrow=3, labels = mylabel)
However, I got error like this:
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ""expression"" to a data.frame
Does anyone know the reason? Is this my programming mistake? OR expression function just could not be used in ggpubr::ggarrange function? Thanks a lot.

Related

function-expression R problem when using curve()

When plot using curve() in R I give a function as an argument.
E.g.
f=function(x) x^2
curve(f,2,3)
I get the curve plotted.
But I have to work with the derivative function D(), which you have to give an expression as argument and I can't get my curve plotted.
This is my code:
#To get the derivative
f1 = expression((x)^2)
d1=D(f1,"x")
#To plot the curve
f1=function(x) eval(f1,"x")
curve(f1,2,3)
And the error is:
Error in eval(f1, "x") : invalid 'envir' argument of type 'character'
How can I fixed? I've tried to plot directly the expression and no result. I can fix the problem if I were able to convert the function to expression, but no clue either.
Thanks in advance, Alberto.
I think you've mistyped because you want to evaluate d1 and NOT f1
#To get the derivative
f1 = expression((x)^2)
d1=D(f1,"x")
#To plot the curve
f=function(x) eval(f1)
curve(f,2,3)
The code above works for me.

Using external variables with ggpairs

I'm writing functions for an R-package which will use a wrapper function for ggpairs from the package GGally to plot the output objects of the methods. I would like ggpairs to be able to use variables not part of the input object for defining aesthetics but this produces an error message with ggpairs, see below for a minimal example:
library(GGally)
library(ggplot2)
# The data object
object <- list(x = iris[, 1:2], label = "Iris data")
# The grouping
y <- iris[, 5]
# The plotting function
wrapper <- function(object, mapping = aes()){
ggpairs(object$x, mapping)
}
# This works
wrapper(object)
# This doesn't work
wrapper(object, aes(color = y))
The latter one produces the error message:
Error in .subset(col, i) : object of type 'symbol' is not subsettable
Any trick to get the second plotting command to work without modifying the input object would be greatly appreciated.

Cannot coerce type 'closure' to vector of type 'double' (polygon)

plot( dnorm , col='white')
polygon( dnorm, col='grey' )
returns the above error message, not on plot, but on polygon.
body(polygon) %>% grep(pattern='numeric') finds only one occurrence on line 4, which doesn't seem to have anything to do with this error. So I'm at a loss as to where to look for the source of the problem.
plot has a function method, whereas polygon does not. From ?plot:
x: the coordinates of points in the plot. Alternatively, a single plotting structure, function or any R object with a plot method can be provided.
Additionally, from ?plot.function, the S3 method to plot functions:
## S3 method for class 'function'
plot(x, y = 0, to = 1, from = y, xlim = NULL, ylab = NULL, ...)
This explains why you get a plot with values from 0 to 1 with plot when you pass dnorm as an argument.
Note functions like dnorm are also known as closures. This explains why you get that error with polygon. Since polygon does not accept functions as an argument, it tries to convert dnorm, a closure, to a vector, but that isn't a valid conversion.
The error in polygon is actually happening in the as.double call within xy.coord:
> polygon(dnorm)
Error in as.double(y) :
cannot coerce type 'closure' to vector of type 'double'
> traceback()
2: xy.coords(x, y)
1: polygon(dnorm)
Note as.double doesn't register in the trace stack because it is a primitive. By looking at the source of xy.coords, you can see where the error is happening. To semi-confirm:
> as.double(dnorm)
Error in as.double(dnorm) :
cannot coerce type 'closure' to vector of type 'double'
dnorm(-3:3) actually produces a numeric vector, which is why that works with polygon.
The call to plot will resolve to a variety of default methods for different types of objects. See methods(plot) for a list in your environment. For dnorm it is plot.function, which takes the function as an argument and provides a set of inputs into the function. Incidentally this will also work with rnorm because plot.function provides a default argument of n=101.
A more common alias for plot.function is curve.
curve(dnorm, col="grey")
The polygon has no such analogous method for various types of objects.
You need to polygon( dnorm(-3:3) ) or whatever the xlim limits are. polygon lacks a method for treating functions (although plot has one).

R shiny - correct use of an input variable, seems like a scope error

I think I have a problem with the scope of input$variable. Following is the code description.
In my ui.R file I have a selectInput that defines the value taken by input$variable in my server.R file. The selectInput is as follows:
selectInput("variable", "Variable:",
c("Handling of Complaints" = "complaints",
"Does not allow special privileges" = "privileges",
"Opportunity to Learn" = "learning",
"Raises based on Performance" = "raises",
"Too Critical" = "critical",
"Advancement" = "advance")),
In my server.R file I have a renderUI that uses this input$variable inside a with and inside a qplot as follows:
grid <- with(attitudeData, expand.grid(
xgrid = seq(min(input$variable), max(input$variable), length = dim(attitudeData)[1]),
ygrid = levels(sex)
))
ggattitude <- qplot(x=input$variable, y=rating,
data=attitudeData, color=sex) + geom_line(data=grid)
attitudeData is the dataframe with variables (aka features or predictors) with names as defined in the selectInput.
If I use any of the variable names, say complaints, directly in the code it works and my plot is generated the way I want for that variable. But with input$variable I can't get it to work. This is why I think the problem has to do with the scope of input$variable.
Any help would be appreciated.
Following is the error I get from shiny which isn't very helpful to me.
Error in seq.default(min(input$variable), max(input$variable), length = dim(attitudeData)[1]) : 'from' cannot be NA, NaN or infinite
If I replace input$variable with complaints in the with, the error for the use in qplot is as follows:
Error in eval(expr, envir, enclos) : object 'complaints' not found
Thanks in advance for any help I get.
You can add print statements to clarify what R sees as input. In your server file:
grid <- with(attitudeData, expand.grid(
print(input$variable)
xgrid = seq(min(input$variable), max(input$variable), length = dim(attitudeData)[1]),
ygrid = levels(sex)
))
ggattitude <- qplot(x=input$variable, y=rating,
data=attitudeData, color=sex) + geom_line(data=grid)
This output will be printed to the R console. I have a feeling that your input isn't rendering as "complaints".
What should min("complaints") return? So you are giving string as argument to function min() it is different from min(complaints) if that is what you want.
Or is there some other problem like:
xgrid = seq(min(input$variable), max(input$variable), length = dim(attitudeData)[1])
I am wondering if you really try to do:
xgrid = seq(min(attitudeData[input$variable]), max(attitudeData[input$variable]), length = dim(attitudeData)[1])

Error ploting sequences in TraMineR R

Just starting to experiment TraMiner, after having read the (very good) User Guide
I managed to create the sequences from my data, but as I'm trying to plot I got the following errors:
> seqiplot(my.data$sequences, title="My first sequences", withlegend = TRUE)
Error in do.call(plot, args = plist) :
'what' must be a string or a function
Where does this come from and what can I do about it?
I think you get an error because you override the plot function. This reproduce the error:
plot <- 1
do.call(plot,list(0))
Error in do.call(plot, list(0)) :
'what' must be a character string or a function
This should ork:
rm(plot)
seqiplot(my.data$sequences, title="My first sequences", withlegend = TRUE)
I guess the error shows up because you restricted your sequence state object to the single variable my.data$sequences. Have you tried my.data instead?

Resources