Histogram in R Plotly: set number of breaks - r

I would like to choose the number of breaks for a plotly histogram in R like the option "breaks" in hist base R.
Data
x <- rnorm(1000)
Base R
hist(x,breaks = 50)
pltoly
library(plotly)
plot_ly(x = ~x, type = "histogram") # how to add breaks option ?
Thanks for your help.

Try this :
library(plotly)
plot_ly(x = ~x, type = "histogram", nbinsx = 30)

I found a custom solution with the xaxis.size option
plot_ly(x = ~x, type = "histogram", xaxis = list(size = diff(range(x))/50))

Related

Reducing space between y-axis and plot title/axis ticks

I am outputting a scatterplot in R using plotly with the code below:
library(tidyverse)
library(plotly)
set.seed(1)
data.frame(state = c(rep(state.name, 2)),
value = sample(1:100, 100)) %>%
plot_ly(x = ~value,
y = ~state,
type = "scatter",
mode = "markers") %>%
layout(title = list(text = "State Information"))
The issue that I am running into is that the code above renders a plot with an excessive amount of space between the y-axis and the plot title and x-axis ticks respectively:
Can anyone tell me how I can shrink this space so that the plot margins are tighter?
Edit: I know that a similar question was asked here, but this relates to a numeric y-axis, not a categorical one:
R Plotly - Large unused space on top and bottom of graph when setting height
We can use the same procedure for a categorical axis.
Please run schema() and navigate: object ► layout ► layoutAttributes ► yaxis ► range:
[...] If the axis type is category, it should be numbers, using
the scale where each category is assigned a serial number from zero in
the order it appears.
library(plotly)
library(datasets)
set.seed(1)
DF <- data.frame(state = c(rep(state.name, 2)),
value = sample(1:100, 100))
plot_ly(
data = DF,
x = ~ value,
y = ~ state,
type = "scatter",
mode = "markers"
) %>%
layout(
title = list(text = "State Information"),
xaxis = list(
range = ~ c(-1, length(unique(value)) + 1)
),
yaxis = list(
range = ~ c(-1, length(unique(state))),
title = list(text = "state", standoff = 0L)) # maybe needed? sets distance between title and labels
)

Anyway to remove the 0 on the y-axis in Plotly R graph?

I have this simple graphic I am making with plotly.
library(plotly)
fig <- plot_ly(x = ~rnorm(50), type = "histogram")
fig
This is the output.
But my goal is this - no 0 on the y-axis.
This could be achieved by setting the tickvals and ticktext manually via layout options. Note that you have to se tickmode="array". One drawback is that this adds some additional space on the left and the top so that you have to additionally set the margins manually.
library(plotly)
set.seed(42)
x <- rnorm(50)
fig <- plot_ly(x = ~x, type = "histogram")
fig %>%
layout(yaxis = list(
tickvals = as.list(seq(0, 20, 2)),
ticktext = as.list(c("", seq(2, 20, 2))),
tickmode = "array"
), margin = list(l = 2, t = 2))

I want to change the name of r and theta in Plotly (R Software)

I need some help in Plotly, i want to change the name of r and theta in scatterpolar, and i don't know how enter image description here
Here is the code :
fig <- plot_ly(
type = 'scatterpolar',
color = I("purple"),
r = radar$CAeuro,
theta = radar$Mois,
name="Chiffre d'affaires mensuel en Euros",
fill = 'toself'
);fig```
Thanks a lot
Try adding hovertemplate to your code.
Here an example for you (with some arbitrary data since I don't have yours):
plot_ly(
type = 'scatterpolar',
color = I("purple"),
r = iris$Sepal.Length,
theta = iris$Sepal.Width,
name="Chiffre d'affaires mensuel en Euros",
hovertemplate = paste('<i>New R</i>: %{r}',
'<br><b>New Theta</b>: %{theta}<br>'),
fill = 'toself'
)

One slider to control graphs in single subplot in R using plotly

I'm looking for certain fix with range selector in plotly using R.
I have two plots visualized via a single subplot using Plotly in R. Now, I need to add a Range Slider/Selector to the complete plot, so that changing it modifies both my plots.
Is it possible via Plotly? (using R only)
This functionality is similar to Dygraphs synchronize feature(https://rstudio.github.io/dygraphs/gallery-synchronization.html).
I'd recommend using subplots option shareX = TRUE:
Please check the following example:
library(plotly)
DF1 <- data.frame(x=1:100, y=runif(100)+ seq(0, 1, length.out = 100))
DF2 <- data.frame(x=1:100, y=runif(100)+ seq(0, 2, length.out = 100))
p1 <- plot_ly(DF1, x = ~x, y = ~y, type = "scatter", mode = "lines+markers")
p2 <- plot_ly(DF2, x = ~x, y = ~y, type = "scatter", mode = "lines+markers")
p <- subplot(p1, p2, nrows = 2, shareX = TRUE)
p

R plotly show xcoordinate on xaxis

I'm trying to create a graph with a similar x-axis format to this (from https://plot.ly/r/line-charts/):
code given:
library(plotly)
x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)
p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')
However, running the code on my machine produces this graph:
Notice that the x-coordinate hover is not there.
R version: 3.4.1
Plotly version: 4.7.1
Changing the hoverinfo and text tags in plot_ly just changes the hoverinfo over the graph. How do I show the same hovering x-coordinate in the first graph?
Update: setting hoverinfo = "text+x" and layout(hovermode = "x") shows the x-coordinate on the x-axis and the point tooltips.
Try setting hovermode to 'x'
p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines') %>% layout(hovermode = 'x')
and it should work.

Resources