plotly bar and line chart - r

I want to plot a bar together with a line chart in R with plotly.
My first attempt was
p <- plot_ly(
x = c(1,2,3,4,5),
y = c(1,2,1.5,3,2),
type='scatter',
mode='lines',
line = list(color = 'black')
)
add_trace(
p,
x = c(1,2,3,4,5),
y = c(0.5,0.7,0.6,0.9,0.8),
type='bar',
marker = list(color = 'red')
)
The result is right, but I get the following warning:
Warning message: The following attributes don't exist: 'mode', 'line'
I guess cause the bar plot in add_trace() cannot handle the line and mode parameter from the plot_ly() function. So I changed the order:
p <- plot_ly(
x = c(1,2,3,4,5),
y = c(0.5,0.7,0.6,0.9,0.8),
type='bar',
marker = list(color = 'red')
)
add_trace(
p,
x = c(1,2,3,4,5),
y = c(1,2,1.5,3,2),
type='scatter',
mode='lines',
line = list(color = 'black')
)
This time I get the following message and red markers are displayed on the black line chart.
A marker object has been specified, but markers is not in the mode
Adding markers to the mode...
How can I fix this? (I'm using the R package plotly 4.1.0)

I'm running plotly 4.0.1, but if I add mode='lines+markers' instead of just mode='lines' the error message goes away for me.
--edit to add full code--
For the lazy (like me), here's the full code that worked on my end:
p <- plot_ly(x = c(1,2,3,4,5),
y = c(0.5,0.7,0.6,0.9,0.8),
type='bar',
marker = list(color = 'red', opacity=0)
)
add_trace(p,
x = c(1,2,3,4,5),
y = c(1,2,1.5,3,2),
type='scatter',
mode='lines+markers',
line = list(color = 'black')
)

Related

Create a filled area line plot with annotated scatters using plotly

I want to create a filled area plot with line and scatters like in the screenshot attached but I do not know how could add scatters for every year of x-axis and also annotate its value. My code is:
library(plotly)
data <- t(USPersonalExpenditure)
data <- data.frame("year"=rownames(data), data)
fig <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', type = 'scatter', mode = 'line', stackgroup = 'one', fillcolor = '#F5FF8D')
fig
The mode lines+markers+text allows you to define a line plot with markers and add some text.
I changed the type of year from factor to numeric, because I had to expand the xaxis for readabilty of the annotations.
library(plotly)
data <- t(USPersonalExpenditure)
data <- data.frame("year" = as.numeric(rownames(data)), data)
plot_ly(data,
x = ~year,
y = ~Food.and.Tobacco,
text = ~Food.and.Tobacco) %>%
add_trace(
type = 'scatter',
mode = 'lines+markers+text',
fill = 'tozeroy',
fillcolor = '#F5FF8D',
marker = list(color = 'black'),
line = list(color = 'black'),
textposition = "top center",
hovertemplate = paste0("<b>%{x}</b>
Cummulative Food and Tobacco: %{y}
<extra></extra>"),
hoveron = 'points') %>%
layout(xaxis = list(
range= list(min(data$year) - 1, max(data$year) + 1)))

Plotly R setting the title of a continuous color legend

I'm trying to plot a 3D scatter using Plotly and R. Other than x, y and z I also would like to set the color of each point depending on a fourth variable.
I manage to set the plot correctly (the use of name = ~res is to show the value of res while hovering), but I am not able to change the name of the colorbar.
This is a mock code of what I've done:
library(tidyverse)
library(plotly)
a = seq(1,10,1)
b = seq(100,1000,100)
c = seq(1,4.9,0.4)
data = tibble(a,b,c)
data <- data %>% mutate(res = a+b+c)
layout_details <- list(xaxis = list(title = 'a [-]'),
yaxis = list(title = 'b [-]'),
zaxis = list(title = 'c [-]'),
coloraxis=list(colorbar=list(title=list(text='Here are the results'))))
p = plot_ly(data, x = ~a, y = ~b, z = ~c, color = ~res, type = 'scatter3d',
mode = 'markers', name = ~res, showlegend = FALSE, scene = 'scene1')
p <- p %>% layout(scene1 = layout_details)
p
I've noticed that a quite similar question was asked (R plotly to legend title value ignored for continuous color scatter plot), but without any answers.
Does anyone know how to solve this?
Thanks
You can define your colorbar inside the marker argument.
The name argument is interfering with the colorbar therefore I moved res from the name argument to the hovertemplate and the customdata.
Code
p = plot_ly(data, x = ~a, y = ~b, z = ~c,
name = "",
scene = 'scene1',
type = 'scatter3d',
mode = 'markers',
customdata = as.list(data$res),
hovertemplate = paste('x: %{x}',
'y: %{y}',
'z: %{z}',
'name: %{customdata}',
sep = "\n"),
marker = list(color = ~res,
colorbar = list(title = "Here are the results"),
colorscale='Viridis',
showscale = TRUE))
p <- p %>% layout(scene1 = layout_details)
p
Plot

R Plotly how to add custom names and colors to the legend only

I am using r plotly to plot a stack bar chart and want to replace the legend that is there with a custom legend with names and colors that I choose. The bottom(gray color) is for the first source to report the intel. The top part of the bars are colored according to what kind of source(there are three of them). So light blue would be source1, blue = source2, and darkblue = source3. I would like to add a custom legend with the four colors:
'rgba(128,128,128,1)' = First Reported
'rgba(0,0,139,1)' = source1
'rgba(173,216,230,1)' = source2
'rgba(0,0,255,0.3)' = source 3
for the legend and keep the rest of the plot the the way it already is. This is my code for the plot so far:
library(plotly)
x <- c("source1","source2","source3","source4","source5","source6","source7")
y <- c(195760, 161,133199, 22597, 5249, 1276, 115)
y2 <-c(0,137, 36147,0,0,1153, 81)
xform <- list(categoryorder = "array",
categoryarray = c("source1","source2","source3","source4","source5","source6","source7"))
plot_ly(x=x, y=y2, type='bar',name = 'First Reported',text = y2, textposition = 'inside',textfont = list(color = '#FFFFFF'),
marker= list(color = c('rgba(128,128,128,1)','rgba(128,128,128,1)',
'rgba(128,128,128,1)','rgba(128,128,128,1)',
'rgba(128,128,128,1)','rgba(128,128,128,1)',
'rgba(128,128,128,1)'))) %>%
add_trace(y=y, name='Not First To Report', text = y,
marker = list(color = c('rgba(0,0,139,1)','rgba(0,0,139,1)',
'rgba(173,216,230,1)','rgba(0,0,255,0.3)',
'rgba(0,0,255,0.3)','rgba(173,216,230,1)',
'rgba(173,216,230,1'))) %>%
layout(title = "Counts by Intel Source",xaxis = xform, barmode = 'stack')
This is what the graph looks like so far:

Boxplot next to a scatterplot in R with plotly

I created a scatter plot with plotly in R. Now I want to plot a boxplot with different data next to the scatter plot. I want to use plotly for this.
The result should look like this. Can someone help me please, I have no idea how to do that.
My code so far is
plot_ly(ds, x = ~x, y = ~y , mode = "markers", name = "Clusters", opacity = point.opacity,
text = ds$id,
hoverinfo = "text",
marker = list(symbol = point.symbol, color = ~color, size = point.size,
line = list(color = "#262626", width = point.linewidth, opacity = point.lineopacity)),
showlegend = F)
Here is an example on how to make a scatter with marginal box plots with plotly:
library(plotly)
data(iris)
create, three plots for the data: one for the scatter, two for the appropriate box plots, and one additional empty plot. Use the subplot function to arrange them:
subplot(
plot_ly(data = iris, x = ~Petal.Length, type = 'box'),
plotly_empty(),
plot_ly(data = iris, x = ~Petal.Length, y = ~Petal.Width, type = 'scatter',
mode = 'markers'),
plot_ly(data = iris, y = ~Petal.Width, type = 'box'),
nrows = 2, heights = c(.2, .8), widths = c(.8,.2), margin = 0,
shareX = TRUE, shareY = TRUE) %>%
layout(showlegend = F)

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