Getting weird error with the function as.ggplot() from ggplotify package - r

This uses the library ggplotify. This allows me to take a base r plot and create a ggplot plot from it with the function as.ggplot().
However, I am receiving an error that doesn't make sense to me when I run the following lines of code.
y <- 1:10
x <- 2:11
length(x)
length(y)
as.ggplot(~plot(x, y))
The error I receive is:
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ.
However, x and y have the same length and when I use plot(x,y) normally, I don't receive any errors. Can someone please explain this to me? Thanks in advance!

It works when you put the values directly in the function:
as.ggplot(~plot(1:10,2:11))
I think it is just limited to working like this.

Related

plot() and dev.off() error message for forestplot()

I've made a figure using forestplot() and I've tried to output it as a high resolution figure but it's not working. The figure plots fine in the R studio plots window, but when I try to use plot() and dev.off() I get an error message.
No idea why.
Thanks for any help!
setwd("C:/Users/User/Desktop")
ppi <- 300
png("plotfp.png", width=9*ppi, height=9*ppi, res=ppi)
plot(plotfp)
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' is a list, but does not have components 'x' and 'y'
> dev.off()

Error with plotly subplot: Error in xy.coords(x, y) : 'x' is a list, but does not have components 'x' and 'y'

I am getting an error when trying to use plotly subplot in R and I cannot figure out why. If I try to plot two plotly plots b and c:
library(plotly)
df <- data.frame("A"=1:10, "B"=11:20, "C"=21:30)
b_plot <- plot_ly(df) %>%
add_trace(x=~A, y=~B)
c_plot <- plot_ly(df) %>%
add_trace(x=~A, y=~C)
they work fine by themselves but when I try to plot them side by side in a subplot:
p <- subplot(b_plot, c_plot)
I get the error:
Error in xy.coords(x, y) : 'x' is a list, but does not have components 'x' and 'y'
I can't even get the Plotly example to work. Any help appreciated, thanks!
Turns out the function was being overwritten by the subplot function from the Hmisc package so changing to plotly::subplot(...) fixes the problem

Adding data labels to boxplot in R

Here's my code:
iFacVector <- as.factor(c(1,1,1,1,10,1,1,1,12,9,9,1,10,12,1,9,5))
iTargetVector <- c(2,1,0,1,6,9,15,1,8,0,1,2,1,1,9,12,1)
bp <- plot(iFacVector,iTargetVector)
text(bp,tapply(iTargetVector,iFacVector,median),labels=tapply(iTargetVector,iFacVector,median),cex=.8)
I am getting the following (classic R) error:
Error in xy.coords(x, y, recycle = TRUE) :
(list) object cannot be coerced to type 'double'
The vectors I am passing are numeric so I don't know what the problem is. I have tried unlist() and as.vector(). I have also tried using bp$stats[3,] as the labels.
The help for text gives the arguments as
text(x, ...)
so the first argument in your code, bp, is being treated as the x coordinate for where to place the text. You can just leave off the bp and get better behavior. You might also want to add pos=3 to get a nicer placement of the text.

R : Plot many boxplot in the same graph using dataframe

Hello everyone and thank you for helping me with R.
i have a 39cols * 168rows matrix which looks like this :
and i want to plot boxplot (1 for each row) in the same graph.
Two hours of intense research on how to make that and i still have no clue.
What i tried (f is the read.csv i got ) :
boxplot(x = as.list(as.data.frame(f)))
qp <- boxplot(x = as.list(as.data.frame(f)))
rn <- as.numeric(rownames(f))
plot(qp,rn)
and i've got :
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
even if i do not know if the result of the plot was the thing i wanted.
If you want one box per row, you need to transpose the matrix before passing it to boxplot:
boxplot(t(f))
This will then do the right thing — provided you’ve read in your data correctly. In your image, this isn’t the case: the header columns are (incorrectly) part of the data. Be sure to pass header = TRUE to your data reading function to fix this.

Manipulate graphics within an R package (eRm)

I am using the eRm package to run the dichotomous Rasch model. My problem is that I can not manipulate the length of the x axis on the person-item map.
The description for the plotPImap command says to use xrange= to specify the range, but when I do so, I get the following error:
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
Here is my code:
Model2 <- RM(EBL_MC)
plotPImap(Model2, xrange=c(-5:5), latdim = "Writing Achievement", main="Person-Item Map (EBL)")
Suggestions?
This is just a shot in the dark, since you haven't provided a reproducible example, but generally when functions in R ask for an axis range argument, they are looking for only two values, the low and high. Try xrange = c(-5,5).

Resources