I'd like to do plotly chart and plot filled area shape and bars on one plot. However area shape overlaying bars. I couldn't change order of elements. Is it possible to bring bars in fron?
data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))
plot_ly(data, x = data$years, y=data$values1, type = 'bar') %>%
add_trace(x=data$years, y=data$values2, type = 'scatter', mode = 'lines', fill = 'tozeroy')
This is adapted from the answer by #Maximilian Peters. This code
data <- data.frame(years = c(2005,2006,2007), values1 = c(1,2,3), values2 = c(3,3,2))
plot_ly(data) %>%
add_trace(x=~years, y=~values1, type = 'bar') %>%
add_trace( x = ~years, y=~values2, type = 'scatter', mode = 'lines', fill = 'tozeroy', yaxis='y2'
) %>%
layout(title = 'Trace order Plotly R',
xaxis = list(title = ""),
yaxis = list(side = 'left', title = 'Y - Axis', overlaying = "y2"),
yaxis2 = list(side = 'right', title = "" )
)
generates this output:
Related
I was wanting to know if there is a way of not showing the add_trace values on the plotly graph output? The code below produces a series of cumulative annual line graphs. The add_trace addition was inserted to add a dashed vertical line to the graph. However, when I hover over the graphic it shows the values for the vertical line - can this be turned off, but allow hover values for the cumulative annual lines? I was hoping to do this with a tweak of the code below.
Thanks.
df %>%
filter(op_area_ocode == 'ALL') %>% mutate(Year = as.character(R.Year), DoY = as.Date(DoY.N, origin = "1972-01-01") - 1) %>%
plot_ly(., x = ~ DoY, y = ~cni, color = ~Year, colors = c("#D3D3D3","#D3D3D3","#D3D3D3","#D3D3D3","black","#E69F00","#56B4E9", "#009E73", "#AA4499"), type = 'scatter', mode = 'lines') %>%
layout(title = paste("Reports"), xaxis = list(title = "", type = "date", tickformat = "%d %b"), yaxis = list (title = "Number of reports")) %>%
add_trace(x = as.Date("1972-08-15"),type = 'scatter', mode = 'lines', line = list(color = 'red', width=0.5, dash ="dot"),name = '', showlegend = FALSE)
You should be able to use hoverinfo='none' in your add_trace() call
df %>%
filter(op_area_ocode == 'ALL') %>% mutate(Year = as.character(R.Year), DoY = as.Date(DoY.N, origin = "1972-01-01") - 1) %>%
plot_ly(., x = ~ DoY, y = ~cni, color = ~Year, colors = c("#D3D3D3","#D3D3D3","#D3D3D3","#D3D3D3","black","#E69F00","#56B4E9", "#009E73", "#AA4499"), type = 'scatter', mode = 'lines') %>%
layout(title = paste("Reports"), xaxis = list(title = "", type = "date", tickformat = "%d %b"), yaxis = list (title = "Number of reports")) %>%
add_trace(x = as.Date("1972-08-15"),
type = 'scatter',
mode = 'lines',
line = list(color = 'red', width=0.5, dash ="dot"),
name = '',
showlegend = FALSE,
hoverinfo="none"
)
I created following 3d plot using plotly in R.
library(plotly)
library(MASS)
mu_v=c(0.5,0.4,0.5,0.2)
sigma_m=matrix(data = c(1,0.5,0.7,0.4,0.5,1,0.8,0.9,0.7,0.8,1,0.5,0.4,0.9,0.5,1),nrow = 4,byrow = T)
data1=mvrnorm( 10000,mu = mu_v , Sigma = sigma_m )
data1=as.data.frame(data1)
colnames(data1)=c("X1","X2","X3","X4")
plot_ly(x=~X1, y=~X2, z=~X3, type="scatter3d", color=~(X4), mode="markers",data = data1
, marker = list(
size = 5,
opacity = 10)) %>% layout(plot_bgcolor = "#bababa")
Is there a way to change the labels of axis and the coloring variable ? Also Can I change the background of the xy,yz and xz surface and increase the plotting area?
Update
I tried to edit the legend and change the background color as follows:
plot_ly(x=~X1, y=~X2, z=~X3, type="scatter3d", color=~(X4), mode="markers",data = data1
, marker = list(
size = 5,
opacity = 10)) %>%
layout(scene = list(legend = list(title = "legend"),plot_bgcolor='#e5ecf6'))
But it didnt work
Here is an option to address your first 2 questions (change x/y/z axis and color legend titles).
plot_ly(
x = ~X1,
y = ~X2,
z = ~X3,
type = "scatter3d",
mode = "markers",
data = data1,
marker = list(
color = ~X4,
colorbar = list(title = "Legend"), # Change colour legend title
colorscale = "Blues", # Change colour palette
size = 5,
opacity = 10)) %>%
layout(
plot_bgcolor = "#bababa",
scene = list(
xaxis = list(title = "Axis 1"), # Change x/y/z axis title
yaxis = list(title = "Axis 2"),
zaxis = list(title = "Axis 3")))
I'm trying to plot overlaid line and bar charts using plotly, grouped by the same feature grp.
I need to use fixed colors for both lines and bars.
It works well when I first add the bar-trace :
library(plotly)
df <- data.frame(year = rep((2000:2017), each=2),
grp = rep(c("Grp1", "Grp2"), 18),
y1 = rnorm(36, 100, 40),
y2 = rnorm(36, 50, 10)
)
plot_ly(df) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp,
colors = colorRamp(c("grey", "black"))) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
But If I switch bar trace and line trace, my color selection for bars disappears.
plot_ly(df) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp,
colors = colorRamp(c("grey", "black"))) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
Obviously my syntax is incorrect : does someone know how to write this code properly to ensure colors are stable whatever the order is ?
Many thanks !
Try to specify colors inside plot_ly:
plot_ly(df, colors = colorRamp(c("grey", "black"))) %>%
add_trace(x = ~year, y = ~y2,
type = 'scatter',
mode = 'lines+markers',
linetype = ~grp,
line = list(color = "red")) %>%
add_trace( x = ~year, y = ~y1,
type = 'bar',
yaxis = "y2",
opacity = .4,
color = ~grp) %>%
layout(yaxis2 = list(overlaying = "y",
side = "right"))
I have the following bar in plotly and I want to :
get the x-axis title away from the axis label so they dont overlap
make the Y-axis labels bigger
bring the bar values to the top of bars
My code is :
library(plotly)
plot_ly(x = c('100-200','200-400', '400-600','600-800','800- 1000'),
y = c(12261,29637,17469,11233,17043),
name = "dd",
type = "bar",
xaxis = list(title ="tr"),
yaxis = list(title = "cc") ,
text = c(12261,29637,17469,11233,17043),textposition = 'auto') %>%
layout(
xaxis = list(tickangle=15, title = " "),
yaxis = list(title = " "))
Thanks for your comments :)
Question 1: get the x-axis title away from the axis label so they dont overlap
This problem can be solved setting proper margins with margin = list(b=100, l=100) in layout.
Question 2: make the Y-axis labels bigger.
Use xaxis = list(titlefont=list(size=30)) in layout
Question 3: bring the bar values to the top of bars.
Use add_text with textposition = 'top'
library(plotly)
x <- c('100-200','200-400', '400-600','600-800','800-1000')
y <- c(12261,29637,17469,11233,17043)
labs <- c(12261,29637,17469,11233,17043)
plot_ly(x = x, y = y,
name = "dd",
type = "bar",
xaxis = list(title ="tr"),
yaxis = list(title = "cc")) %>%
add_text(x=x, y=y, text=labs, hoverinfo='none', textposition = 'top', showlegend = FALSE,
textfont=list(size=20, color="black")) %>%
layout(margin = list(b=100, l=100),
xaxis = list(tickangle=15, title = "Lenght of exon", titlefont=list(size=30)),
yaxis = list(title = "Number of genes", titlefont=list(size=30)))
Note that your graph is not exactly reproducible with the given code at the moment, so let me know if I have made unnecessary assumptions.
TLDR: Look at the bottom if you prefer all the changes described in the question.
I would recommend not using tickangle as it screws things up a bit. Try the following. Note the use of insidetextfont and tickfont for the yaxis.
library(plotly)
x = c('100-200','200-400', '400-600','600-800','800- 1000')
y = c(12261,29637,17469,11233,17043)
plot_ly(
x = x, y = y, type = "bar", text = y, textposition = 'auto',
insidetextfont = list(color = 'white')
) %>% layout(
xaxis = list(title = "Length of exon"),
yaxis = list(title = "Number of genes", tickfont = list(size = 15))
)
If you want to make the labels lie outside the bars, then use textposition = 'outside' instead of textposition = 'auto', and get rid of the insidetextfont for the default black colour. That may end up messing with the range of the y axis and you would need to manually define the range which may be cumbersome.
plot_ly(
x = x, y = y, type = "bar", text = y, textposition = 'outside'
) %>% layout(
xaxis = list(title = "Length of exon"),
yaxis = list(
title = "Number of genes", tickfont = list(size = 15),
range = c(0, max(y) + 2000)
)
)
This gives us .
I do not recommend tickangle, but if you must, use it with margin in layout.
plot_ly(
x = x, y = y, type = "bar", text = y, textposition = 'outside'
) %>% layout(
xaxis = list(title = "Length of exon", tickangle = 15),
yaxis = list(
title = "Number of genes", tickfont = list(size = 15),
range = c(0, max(y) + 2000)
), margin = list(b=100)
)
Hope this helps.
Here is my data:
set.seed(42)
mydata = data.frame(A = rnorm(20), B = rnorm(20), Index = sample(190:400,20))
I am trying to divide the data into 20 different intervals based on the Index value and then color the scatter points according to their interval value. Below is my code. It is not working perfectly.
cols = colorRampPalette(c("red", "black"), space = "rgb")(20)
mydata$interval = cut(mydata$Index,breaks = 20)
mydata$cols = cols[mydata$interval]
require(plotly)
x = list(title = "A")
y = list(title = "B")
plot_ly(mydata, x = ~A, y = ~B, color = ~cols, type = "scatter",
mode = 'markers', hoverinfo = 'text',
text = ~paste(interval)) %>%
layout(xaxis = x, yaxis = y)
How do I get a colorbar in the legend where the colors are based on Index value.
Are you looking for this:
plot_ly(mydata, x = ~A, y = ~B, type = "scatter",
mode = 'markers', hoverinfo = 'text', colors = colorRampPalette(c("red", "black"), space = "rgb")(20), color = ~Index, text = ~paste(interval), marker = list(size=14)) %>%
layout(xaxis = x, yaxis = y) %>%
colorbar(title = "My Legend")