autoplot does not accept ts object - r

I am creating a ts object and then I am trying to run it through autoplot. Execution gives me an error:
autoplot(pts, facets = TRUE)
Error: Objects of type mts/ts/matrix not supported by autoplot.
I have already checked the type of the object , and it is ts and autoplot is supposed to make a plot out of the ts object. I also tried to run other built in ts object (USAccDeaths) , but it gives me same error
library(ggplot2)
pts <- ts(data = Popcopys[,-1], start = c(2006,1),frequency = 1 )
autoplot(pts)
autoplot(USAccDeaths)
A plot of TS is expected , but what I get is this error:
autoplot(pts)
Error: Objects of type mts/ts/matrix not supported by autoplot.
autoplot(USAccDeaths)
Error: Objects of type ts not supported by autoplot.

This works:
library(ggplot2)
library(ggfortify)
autoplot(USAccDeaths)
Following https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_ts.html :
"{ggfortify} lets {ggplot2} know how to interpret ts objects"

If you have ggplot or tidyverse loaded into your environment along with the forecast library both have an autoplot function.
You will need to specify which you are attempting to use.
library(forecast)
library(ggplot2)
data <-read.csv('C:/users/person/desktop/data.csv')
ts_df <-ts(data, start = 2018, frequency = 52)
forecast::autoplot(ts_df[,'Column_name'])

Related

plotRGB error when calling plot_map() in rayshader package in R

I'm trying to use the rayshader package in R to plot elevation data, but I'm getting an error when I call the plot_map() function. I can't even get the example to run without running into the following error. Any ideas on how to get this function to run?
error in evaluating the argument 'x' in selecting a method for function 'plotRGB': unused argument (asp = 1)
Here is the example code that I am calling.
library(rayshader)
#Here, I load a map with the raster package.
loadzip = tempfile()
download.file("https://tylermw.com/data/dem_01.tif.zip", loadzip)
localtif = raster::raster(unzip(loadzip, "dem_01.tif"))
unlink(loadzip)
#And convert it to a matrix:
elmat = raster_to_matrix(localtif)
#We use another one of rayshader's built-in textures:
elmat %>%
sphere_shade(texture = "desert") %>%
plot_map()

Creating plot using plotnine of python from R through reticulate

Being a R user, I am learning to incorporate python command in R through reticulate, I tried plotting graph using the plotnine package in R but it returned the following error, can anyone help?
library(reticulate)
library(ggplot2)
pd <- import('pandas', as='pd',convert=FALSE)
p9 <- import('plotnine')
mpg_py <- r_to_py(mpg,convert=FALSE)
mpg_pd <- pd$DataFrame(data=mpg_py)
p9$ggplot(data=mpg_pd,p9$aes(x='displ',y='cty'))
# Error in py_call_impl(callable, dots$args, dots$keywords) :
# AttributeError: 'NoneType' object has no attribute 'f_locals'
The answer to this question pointed me down a promising path for this error. It seems the error is due to an issue in the patsy package that handles the namespace/scoping for plotnine. By default the plotnine.ggplot constructor creates an environment to know where to get the plotting data and aesthetics. So adapting from the linked answer, here's a potential solution where we import the patsy package and use the environment parameter in the ggplot function to pass an evaluation environment (docs).
library(reticulate)
library(ggplot2)
pd = import('pandas',convert=F)
p9 = import('plotnine')
# new imports
patsy = import('patsy')
# import to be able to show in RStudio (see issue here: https://github.com/rstudio/rstudio/issues/4978)
matplotlib = import('matplotlib')
matplotlib$use('tkAgg')
plt = import('matplotlib.pyplot')
mpg_py <- r_to_py(mpg,convert=FALSE)
mpg_pd <- pd$DataFrame(data=mpg_py)
plot_py = p9$ggplot(mpg_pd,p9$aes(x='displ',y='cty'),
# new code (-1 was the only value that didn't throw an error)
environment = patsy$EvalEnvironment$capture(eval_env=as.integer(-1)))
print(class(plot_py)) # "plotnine.ggplot.ggplot" "python.builtin.object"
# Actually show the plot
plot_py
plt$show()

Unable to plot hclust object with ggtree

I am trying to plot a dendrogram from a hclust object with ggtree but I keep getting the same error message:
Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class hclust
I have been extensively looking for a solution, but found none. Furthermore, I have learned that ggtree does support hclust objects, which has confused me even more. From here:
The ggtree package supports most of the hierarchical clustering
objects defined in the R community, including hclust and dendrogram as
well as agnes, diana and twins that defined in the cluster package.
I have borrowed a reproducible example from the link above:
hc <- hclust(dist(mtcars))
p <- ggtree(hc, linetype='dashed')
Which, again, gives me the abovementioned error. If I use rlang::last_error() to get a bit of context, I get:
<error/rlang_error>
`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class hclust
Backtrace:
1. ggtree::ggtree(hc, linetype = "dashed")
3. ggplot2:::ggplot.default(...)
5. ggplot2:::fortify.default(data, ...)
And if I use rlang::last_trace() to get a bit further information:
<error/rlang_error>
`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class hclust
Backtrace:
x
1. \-ggtree::ggtree(hc, linetype = "dashed")
2. +-ggplot2::ggplot(...)
3. \-ggplot2:::ggplot.default(...)
4. +-ggplot2::fortify(data, ...)
5. \-ggplot2:::fortify.default(data, ...)
But I can really see what is wrong...
I have managed to solve my issue. I will post it in case it is of help for someone else in the future.
Apparently, I was running an older version of ggtree which was not beeing correctly updated when I reinstalled ggtree from Bioconductor, I am not sure why. Anyway, I tried to reinstall it by explicitly setting the version argument in the installation call:
BiocManager::install("ggtree", version = "3.10")
And then I could successfully run my reproducible example:
hc <- hclust(dist(mtcars))
p <- ggtree(hc, linetype='dashed')
Note that as a previous workaround, I had managed to plot the hcluster object with ggtree by transforming it to a phylo object with the as.phylo() function from the ape package:
hc <- hclust(dist(mtcars))
hc <- ape::as.phylo(hc)
p <- ggtree(hc, linetype='dashed')

Error - "Objects of type tbl_df/tbl/data.frame not supported by autoplot."

Trying my hands on autoplot and ggseasonplot functions, but neither working. Please guide / help.
library(readxl)
new<-read_excel('NEW DATA.xlsx')
View(new)
library(ggplot2)
autoplot(new)
class(new)
ggseasonplot(new)
Error: Objects of type tbl_df/tbl/data.frame not supported by autoplot.
From my understanding, time series does not support data.frame and has to be converted into time series format. The reason this happens, as you can not tell R to convert the whole table / matrix / data frame to plot, without giving it the constraints, telling what to plot, and how to plot.
I used
my1 <- ts (name of the data frame, [,2], start = year,
month, date, frequency = in my case it was 31)
As I only wanted to plot my column 2, I wrote [,2].
my1 <- ts (data1, [,2], start = 1981,1,1, frequency = 31)
This converted it to time series, which I plotted later by commands-
Autoplot (my1)
ggseasonplot (my1).

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.

Resources