Plotly - unused arguments error - appears inconsistent - r

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.

Related

How to reduce the size of a plot in Jupyter Notebook?

I'm trying out R with Jupyter Notebook and for some reason the plots are huge. How can I reduce the size of the plot? I tried changing the plot_scale option via: options(jupyter.plot_scale=.25) but nothing happened. I've tried other numbers besides .25, but it has no effect.
I also tried
options(repr.plot.width = 1, repr.plot.height = 0.75, repr.plot.res = 100)
as well as options(jupyter.plot_mimetypes = c("text/plain", "image/png" )), but both gave the following error:
Error in value[[3L]](cond): invalid graphics state
Traceback:
plot without title
Ideally, I'm looking for a global solution (i.e. one that changes the default plot size, rather than having to individually scale each plot). Any suggestions?
Try the following:
options(repr.plot.width = 5, repr.plot.height = 5)
x = seq(0,2*pi, length = 50)
y = sin(x)
plot(x,y)
Then compare it to the output from the following in another cell or after restarting the kernel:
options(repr.plot.width = 15, repr.plot.height = 15)
x = seq(0,2*pi, length = 50)
y = sin(x)
plot(x,y)
You should see a difference.
If you set the width and height to you get plot without title error.
Should be good with 2 or above because ~1.8 or above worked in my tests.
Based on this post.
simply run
dev.off()
then rerun your code for plotting.
Alternativly use ggplot. If the error persists you should still try dev.off() and rerun your code.
Otherwise you still have the option to restart your notebook or reinstall R.

Animating Histograms with plotly

I'm trying to create an animated demonstration of the Law of Large Numbers, where I want to show the histogram converging to the density as the sample size increase.
I can do this with R shiny, putting a slider on the sample size, but when I try to set up a plotly animation using the sample size as the frame, I get an error deep in the bowels of ggploty. Here is the sample code:
library(tidyverse)
library(plotly)
XXX <- rnorm(200)
plotdat <- bind_rows(lapply(25:200, function(i) data.frame(x=XXX[1:i],f=i)))
hplot <- ggplot(plotdat,aes(x,frame=f)) + geom_histogram(binwidth=.25)
ggplotly(hplot)
The last line returns the error. Error in -data$group : invalid argument to unary operator.
I'm not sure where it is suppose to be getting data$group (this value has been magically set for me in other invocations of ggplotly).
Skipping the initial ggplot and going straight to plotly, does this work for you?
plotdat %>%
plot_ly(x=~x,
type = 'histogram',
frame = ~f) %>%
layout(yaxis = list(range = c(0,50)))
Or, using your original syntax, we can add a position specification that seems to prevent the bug. This version looks better, with standard ggplot formatting and tweened animation.
hplot <- ggplot(plotdat, aes(x, frame = f)) +
geom_histogram(binwidth=.25, position = "identity")
ggplotly(hplot) %>%
animation_opts(frame = 100) # minimum ms per frame to control speed
(I don't know why this fixes it, but when I googled your error I saw a plotly issue on github that was solved by specifying the position, and it seems to fix the error here too. https://github.com/plotly/plotly.R/issues/1544)

Plotly code - viewer graph doesn't showup

I am running the example code for the Slider function (example 2) in R:https://plot.ly/r/animations/
I want to simply reproduce the given example.
However, the graph doesn't show up in my viewer.
This is the codeI run in R:
library(plotly)
library(gapminder)
p <- gapminder %>%
plot_ly(
x = ~gdpPercap,
y = ~lifeExp,
size = ~pop,
color = ~continent,
frame = ~year,
text = ~country,
hoverinfo = "text",
type = 'scatter',
mode = 'markers'
) %>%
layout(
xaxis = list(
type = "log"
)
)
I didn't change it from the way it is described here.
Can one help me and tell me why the graph doesn't show up?
I tried to run the code at start without success as well.
Basically the issue was :
Evaluation error: `as_dictionary()` is defunct as of rlang 0.3.0 Please use `as_data_pronoun()` instead.
I think you just need to update your dplyr to the latest version 0.7.7.
install.packages("dplyr") #or update current version
Also check that :
Any package larger than a few Kb gives the message:
Warning in install.packages :
unable to move temporary installation ‘C:\Users[myname]\R\win-library\3.4\file2b884fc37c13\packagename’ to ‘C:\Users[myname]\R\win-library\3.4\packagename’
If so,Type in : trace(utils:::unpackPkgZip,edit = T)
and edited Line 140 Sys.sleep(0.5) to Sys.sleep(2).
Then you can Run your code and print p
Check out the plot
btw a real cool one !

R plot_ly hoverinfo

I'm using plot_ly in a shiny app. I've the following function
plot_ly(df, x = ~x, y = ~y , key = df$id, type='scatter', mode="markers",
name = "Clusters", opacity = point.opacity, text = df$id, hoverinfo = "text")
If the length of df$id is not 0 then everything is fine. If the length is 0 because no data is available I get the following error: Error in : Column 'hoverinfo' must be length 0, not 1. If I omit hoverinfo, then everything is fine with the empty dataset.
How can I fix this? I tried to use ifelse but that doesn't work. My solution is to define a if statement and then call plot_ly with or without hoverinfo. Does anybody have an better idea?

error in rmarkdown when plotting

I am new to this forum and new to R in general.
But recently I was introduced to rmarkdowns in Rstudio and I have been getting a script ready that uses some csv files to run some calculations and then creates some plots.
Scrip as follows (data attached):SE_MACover_Abr2014_40m_CP.csv
```{r prepare the data}
df <- read.csv(file.choose()) #SE_MACover_Abr2014_40m_CP.csv
# call the libraries
library(ggplot2)
library(plyr)
library(reshape2)
str(df)
df
## create factor levels
df$Stat <-factor(df$Stat, levels = c("SE_Mean", "SE_Min","SE_Max"))
df$Imgs <- factor(df$Imgs, levels = c("2", "5","10", "20","30", "40", "50", "60", "70"))
df$Stat
df$Imgs
```{r plot means, mins, and maxs}
Plot1 <- ggplot(data = df, aes(x = Imgs, y = X, group = Stat)) +
geom_line(aes(linetype = Stat, colour = Stat), size = 1) +
geom_point(aes(colour=Stat)) +
ylab(bquote('Standard Error ')) +
xlab(bquote('Number of Images'))
Plot1
I tried this in R base and worked fine, but rmarkdown in Rstudio the plots do not plot and it gives me the following error message:
Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, : invalid 'filename'
looking at the traceback it shows the following:
stop("invalid 'filename'")
(function (filename = "Rplot%03d.png", width = 480, height = 480, units = "px", pointsize = 12, bg = "white", res = NA, family = "sans", restoreConsole = TRUE, type = c("windows", "cairo", "cairo-png"), antialias = c("default", "none", "cleartype", "gray", "subpixel")) ...
do.call(what = png, args = args)
.rs.createNotebookGraphicsDevice(filename, height, width, units, pixelRatio, extraArgs)
(function () { .rs.createNotebookGraphicsDevice(filename, height, width, units, pixelRatio, extraArgs) ...
grid.newpage()
print.ggplot(x)
function (x, ...) UseMethod("print")(x)
I even tryied plotting the simplest graph with this code:
x <- c(1,2,3,4,5,6)
y <- c(1,2,3,4,5,6)
plot(x,y)
While I was trying to work this out as I thought there was a problem with my script,
someone suggested to paste the piece of script for plotting straight into the console.
I did so and it worked!
And it produces the same error in rmarkdown, but it runs fine in the console..
I don't know how to fix this so I can run my markdown file and it will create the graphs I need,
Please help me
This issue often arises when temporary paths plus filenames created by RStudio when generating the rmarkdown document are too long. On Windows systems, this is generally 260 characters long, but the exact length depends on whether your disk is formatted using FAT, NTFS, etc. Note that the problem is temporary files created by RStudio--you generally can't control these.
However, you can control the length of the path of your rmarkdown documengt. If it is short enough, it will leave "space" for RStudio to create the temporary file name.
Alternatively, restarting RStudio often works, although when working on the rmarkdown document, if you run into the problem again you'll have to restart again.
I had the same issue and I just realized this was due to the filename of my Rmd file--I used a % in the name. The issue disappeared after removing the symbol. What's the filename of your Rmd file? Maybe you should try to rename your file.

Resources