How can I create multiple graphs with plotly on RStudio - r

I am using Plotly package on RStudio. I try to use the function windows() that pops up a new window where the plot should be inserted. This function works well with the normal R function plot, ggplot... but it is not working with plotly graphs.
Here is my code:
windows()
p <- as.doubleplot_ly(data = prices_st, y = prices_st$values, group = prices_st$ind) %>%
layout(showlegend = FALSE)
And the error message is
Error: (list) object cannot be coerced to type 'double'

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()

Changing font size causes gpar error with R histogram plot

My histogram causes an error when I try to change the subtitle fontsize.
hist(data, breaks=18, col = "azure3", main = "$$$$", sub = "$$$$", sub_gp=gpar(fontsize=3), main_gp=gpar(fontsize=13))
I get the following error: "Error in gpar(fontsize=3)": Could not find function "gpar" Calls: Anonymous...hist.default -> plot -> plot.histogram ->plot.window"
I find it strange because I am using the exact same code for the fontsizing of a mosaic plot, in another R markdown file and there it works fine.

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

Plotly - unused arguments error - appears inconsistent

I am creating a Rmarkdown document that contains a number of plots created with plotly.
I cannot figure out why one of my plots is throwing an 'unused arguments' error, as the plot I create before it, which using the same arguments but a different subset of data, works fine. I want to use these 2 plots in a subplot.
Here's what I've got:
df_subset1_p <-
plot_ly(df_subset1, x = ~Month, y = ~data.percent, width = 800, height = 500) %>%
add_lines(color = ~cat) %>%
layout(xaxis = x, yaxis = y, margin = m)
df_subset2_p <-
plotly(df_subset2, x = ~Month, y = ~data.percent, width = 800, height = 500) %>%
add_lines(color = ~cat) %>%
layout(xaxis = x, yaxis = y, margin = m)
Before I can even call the subplot, df_subset2_p throws the error:
Error in plotly(df_subset2, x = ~Month, y = ~data.percent, : unused arguments (x = ~Month, y = ~data.percent, width = 800, height = 500)
I get the error on the 2nd plot, even if I try to run it first. The error reproduces if I just run the script and not the RMarkdown.
The structure of the dataframes looks fine to me. Month is a factor and data.percent is numeric for both.
I tried removing width, height and layout options, same error.
The error reproduces if I run the script without Rmarkdown.
I haven't found this exact problem reported by others on SO, though there are some similar complaints suggesting a compatibility issue between plotly and ggplot (older versions) or that another loaded package is using the same function name. But I don't see how this can be the case here, since I have many previous plots in the notebook that work fine.
df_subset2_p <-
plotly([...])
should be:
df_subset2_p <-
plot_ly([...])
Edit:
See ?plotly and ?plot_ly
The error was all in the typo. (Sighs.)
plot_ly initiates a plotly visualization, whereas plotly is a deprecated function previously used to store plotly account credentials.
Always use plot_ly.

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