I'm trying to overlay a barplot with a scatter plot in plotly. I've managed to plot both and everything looks nice using only markers, but I need to join the markers. Using lines+markers it joins every point with each other, like a closed path. I suppose the problem is that my x-axis variable is categorical and I don't know how to fix it. I can't write the original code but is something like:
to_plot2[, cat_var := as.factor(cat_var)]
pp2 = plot_ly(to_plot2) %>%
add_trace(x = ~cat_var, y = ~var_1, type = 'bar', name = 'bar',
marker = list(color = '#C9EFF9'),
hoverinfo = "text") %>%
add_trace(x = ~cat_var, y = ~var_2, type = 'scatter', mode = 'lines+markers', name = 'lines', yaxis = 'y2',
hoverinfo = "text") %>%
layout(title = 'foo',
xaxis = list(title = "", type = "category",
categoryorder = "'array'"),
yaxis = list(side = 'left', showgrid = FALSE, zeroline = FALSE),
yaxis2 = list(side = 'right', overlaying = "y", showgrid = FALSE, zeroline = FALSE))
and the output is:
Any help?
Thanks in advance
I've just solved the problem. Setting cat_var as key it reorders the dataframe in the appropiate manner.
Thanks!
Related
This question already has an answer here:
Add jitter to box plot using markers in plotly
(1 answer)
Closed 9 days ago.
I would like to obtain exactly the same result as the one presented here in the best answer of this post: Add jitter to box plot using markers in plotly, but without the boxplot itself keeping only the jitter points.
Is there a way to achieve this?
thank you for your answers.
Remove the add_trace. Using the literal code from that answer,
library(plotly)
set.seed(42)
dat <- data.frame(xval = sample(100,1000,replace = TRUE),
group = as.factor(sample(c("a","b","c"),1000,replace = TRUE)))
dat %>%
plot_ly() %>%
# add_trace(x = ~as.numeric(group),y = ~xval, color = ~group, type = "box",
# hoverinfo = 'name+y') %>%
add_markers(x = ~jitter(as.numeric(group)), y = ~xval, color = ~group,
marker = list(size = 6),
hoverinfo = "text",
text = ~paste0("Group: ",group,
"<br>xval: ",xval),
showlegend = FALSE) %>%
layout(legend = list(orientation = "h",
x =0.5, xanchor = "center",
y = 1, yanchor = "bottom"
),
xaxis = list(title = "Group",
showticklabels = FALSE))
We see:
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
I am given a plotly object containing n subplots and I want to add a rangeslider to it. I do so using the code
p<- layout(p, xaxis = list(rangeslider = list(type = "date")))
However, I need to retain the ability to rescale the y axis on all the subplots. I know I can do so by again changing the layout in the following way:
p<- layout(p,
yaxis = list(fixedrange = F),
yaxis2 = list(fixedrange = F),
yaxis3 = list(fixedrange = F),
...)
However, since the number of plots changes, I need a programmatic way of doing that. Note that the plotly object is generated using ggplotly, and as such I don't have access to the individual subplots to pass the layout options there.
I tried passing a list to layout, but it doesn't seem to produce the right result:
layoutlist <- list(yaxis = list(fixedrange = F), yaxis2 = list(fixedrange = F), yaxis3 = list(fixedrange = F), ...)
p<- layout(p, layoutlist)
does not return an error but also doesn't apply the layout options properly.
I also tried using unquoting (!!) to solve the issue but I couldn't get it to work.
Here is some sample code for replication:
(This part can't change)
subplots_list <- list()
for(i in 1:5){
subplots_list[[i]] <- plot_ly()
}
p<- subplot(subplots_list, nrows = 5, shareX = T)
Expected result
p<- p %>% layout(xaxis = list(rangeslider = list(type = "date")))
p<- p %>% layout(
yaxis = list(fixedrange = F),
yaxis2 = list(fixedrange = F),
yaxis3 = list(fixedrange = F),
yaxis4 = list(fixedrange = F),
yaxis5 = list(fixedrange = F)
)
Thanks in advance for your help.
I am visualizing a horizontal bar chart using plot_ly(). The axis line for the x-axis is not displayed but is displayed for the y-axis. I am not sure of how to hide the axis line for a bar chart.
The dataframe used is as follows:
df <- data.frame("Grade" = c(9,10,11,12), "totalHours" = c(93,81,7,96),
"Count" = c(30,16,1,14), "average" = c(3.100,5.062,7.000,6.857))
The plot_ly() call used for visualizing is as follows:
plot_ly(df, x=df$average, y=df$Grade,
type="bar", color=~Grade, orientation = 'h') %>%
add_text(text=round(df$average), hoverinfo='none', textposition = 'auto', showlegend = FALSE,
textfont=list(size=12, color="black")) %>%
layout(yaxis = list(showgrid = FALSE),showlegend=FALSE)
Is there any solution for this?
I think the computer still understands it as xaxis even if it is turned. I hope this does the trick for you:
Noax <- list(
title = "",
zeroline = FALSE,
showline = FALSE,
showticklabels = FALSE,
showgrid = FALSE
)
plot_ly(df, x=df$average, y=df$Grade,
type="bar", color=~Grade, orientation = 'h') %>%
add_text(text=round(df$average), hoverinfo='none', textposition = 'auto', showlegend = FALSE,
textfont=list(size=12, color="black")) %>%
layout(xaxis = Noax)
I'm trying to overlay a line chart and bar chart in plotly (with a vertical line designating an important date) and I'm encountering this issue where the two zero lines are offset instead of on the same line. I've tried messing around with the overlaying = 'y' option within layout and tried changing the order of the three trace components but nothing seems to help. Any ideas how to fix? Below is my code with dummy data:
(Also, bonus points if you can fix my legend-overlapping-y2axis issue)
date <- seq(as.Date("2015/6/1"), by = "month", length.out = 19)
wires_mnth <- c(rep(0,8),100000,750000,1200000,2500000,3100000,5500000,7500000,8000000,9900000,11300000,11000000)
wires_cnt <- c(rep(0,8),100,200,250,325,475,600,750,800,1000,1150,1200)
data <- data.frame(date, wires_mnth)
plot_ly(data) %>%
add_trace(x = ~date, y = ~wires_cnt, type = 'bar', name = 'Wires Count',
yaxis = 'y2', opacity = .5) %>%
add_trace(x = ~date, y = ~wires_mnth, type = 'scatter', mode = 'lines', name
= 'Monthly Wires') %>%
add_trace(x = c(2016,2016), y = c(0, 12000000), type = 'scatter', mode =
"lines", name = 'Sanctions Removed') %>%
layout(title = 'Monthly Aggregate Wire Transfers to Iran',
xaxis = list(title = ''),
yaxis = list(side = 'left', title = 'Wire Amounts (USD)', showgrid =
FALSE, zeroline = FALSE),
yaxis2 = list(side = 'right', overlaying = 'y', title = 'Wires Count',
showgrid = FALSE, zeroline = FALSE)
)
You could add rangemode='nonnegative' to your layout or specify the range manually via range=list(0, max(wires_mnth).
For your bonus question, you can set the x-position of the legend, e.g.
legend = list(x = 1.2)