I would like to add a text-box or information text on plot generated with plotly.
It seems like that now.
without text
I want to make it with text-box as follows (no need to circle:))).
with text-box
The code so far:
p <- plot_ly(data=data.mosann, x = ~Combined, y = ~Actual)%>%
layout(shapes=list(type='line', x0= 0, x1= 235, y0=0, y1=235, line=list(color='red',width=3)),
title = 'Scatter Plot Measurements-Combined Improvements MinT',
xaxis = list(title = "Combined MinT", showgrid = TRUE),
yaxis = list(title = "Measurements", showgrid = TRUE))
p
Try this. Change the x and y for layout annotation based on where you want your text.
p <- plot_ly(data=data.mosann, x = ~Combined, y = ~Actual)%>%
layout(xaxis = ~Combined, yaxis = ~Actual,
annotations = list(text = "YOUR TEXT HERE", x = 200, y = 30,showarrow=FALSE ),
shapes=list(type='line', x0= 0, x1= 235, y0=0, y1=235, line=list(color='red',width=3)),
title = 'Scatter Plot Measurements-Combined Improvements MinT',
xaxis = list(title = "Combined MinT", showgrid = TRUE),
yaxis = list(title = "Measurements", showgrid = TRUE))
Related
I created a graph with two traces in plotly and I use the hovermode x unified. At some point there is no data for line 2, but there is for line 1. In the hoover I only want to see the information about line 1 then, but it also shows information about the nearest point of line 2. When I zoom in the issue dissapears.
So in short, I want to disable the closest function of the hovermode. How can I do this?
Some sample data + code:
library(plotly)
# dummy data
df_data1 = data.frame(date_input = seq(as.Date('2020/01/01'), as.Date('2022/07/01'), by="week")
, value=1:131)
df_data2 = data.frame(date_input = seq(as.Date('2020/01/01'), as.Date('2022/06/01'), by="week")
, value2=3:129)
plot1 <- plot_ly()
plot1 <- plot1 %>%
add_trace(data = df_data1, x = ~date_input, y = ~value
, type = 'scatter', mode = "lines", yaxis = "y", line = list(color = '#E64B35FF', opacity = 0.8)
, showlegend = FALSE
, hovertemplate = ~paste('# Value1: %{y:.0f}<extra></extra>')) %>%
add_trace(data = df_data2, x = ~date_input, y = ~value2
, type = 'scatter', mode = "lines", yaxis = "y2", line = list(color = '#4DBBD5FF', opacity = 0.8)
, showlegend = FALSE
, hovertemplate = ~paste('# Value2: %{y:.0f}<extra></extra>'))
plot1 %>%
layout(
font = list(size = 10),
xaxis = list(title = list(text = '<b>Date<b>', font = list(size = 12))
, fixedrange = T, showgrid = FALSE, ticks = 'inside', type = 'date'
),
yaxis = list(title = list(text = '<b>Number of value1<b>', font = list(size = 12, color = '#E64B35FF'))
, fixedrange = T
, rangemode = 'tozero', showgrid = FALSE, showline = T, ticks = 'inside'),
yaxis2 = list(overlaying = 'y', side = 'right', fixedrange = T, rangemode = 'tozero', showgrid = FALSE, showline = T, ticks = 'inside'
, title = list(text = '<b>Number of value2<b>', font = list(size = 12, color = '#4DBBD5FF'))),
hovermode = "x unified"
)
As you can see the hovermode shows the value2 information still at 15 june (but it shows the closest data from 1 june.
I am using plotly to plot a scatter plot with line+markers. The problem is that I have a categorical x-axis and I want to fix it for full range static on all plots.
Below is the example code:
import(plotly)
x_group <- c('[8-9]','[9-10]','[10-11]','[11-12]','[12-13]','[13-14]','[14-15]','[16-17]','[17-18]','[18-19]','[19-20]')
total <- c('22','17','27','8 ','16','4 ','17','12','15','2 ','22')
example_df <- as.data.frame(list(hour_bin_grp= x_group, total = total))
m <- list(
l = 50,
r = 50,
b = 100,
t = 100,
pad = 4
)
hour_bins <- c("[0-7]","[7-8]","[8-9]","[9-10]","[10-11]", "[11-12]","[12-13]", "[13-14]", "[14-15]", "[15-16]","[16-17]", "[17-18]","[18-19]", "[19-20]", "[20-21]","[21-22]")
p <- plot_ly(example_df, x = ~hour_bin_grp, y = ~total, type = 'scatter', mode = 'markers+lines') %>%
layout(autosize = T, margin = m,
xaxis = list(
type='category',
categoryorder= 'array',
showgrid = TRUE,
autorange = FALSE,
showticklabels = TRUE,
categoryarray= unique(hour_bins)
),
yaxis = list(
showgrid = TRUE,
autorange = TRUE,
showline = TRUE,
showticklabels = TRUE,
rangemode = "tozero",
))
p
The plot seems okay, but I want to fix the range of x-axis to values of hour_bins.
I have already looked up some of the articles on the same, but it does not seem to work under my condition:
Plotly.js: Cannot show full categorical x-axis
https://community.plot.ly/t/cannot-re-arrange-x-axis-when-axis-type-is-category/1274/3
As a workaround I'd suggest using numeric ticks with character ticktext:
library(plotly)
bin_df <- data.frame(hour_bins = c("[0-7]","[7-8]","[8-9]","[9-10]","[10-11]", "[11-12]","[12-13]", "[13-14]", "[14-15]", "[15-16]","[16-17]", "[17-18]","[18-19]", "[19-20]", "[20-21]","[21-22]"))
bin_df$hour_bin_grp <- seq_len(nrow(bin_df))
example_df <- data.frame(hour_bin_grp = bin_df$hour_bin_grp[bin_df$hour_bins %in% c('[8-9]','[9-10]','[10-11]','[11-12]','[12-13]','[13-14]','[14-15]','[16-17]','[17-18]','[18-19]','[19-20]')], total = as.numeric(c('22','17','27','8','16','4','17','12','15','2','22')))
p <- plot_ly(example_df, x = ~hour_bin_grp, y = ~total, type = 'scatter', mode = 'markers+lines') %>%
layout(xaxis = list(
tickmode = "array",
tickvals = bin_df$hour_bin_grp,
ticktext = example_df$hour_bins,
range = list(min(bin_df$hour_bin_grp), max(bin_df$hour_bin_grp))
))
p
https://community.plot.ly/t/how-to-plot-multiple-x-axis-in-plotly-in-r/3014/3?u=him4u324
I have posted my question on Plotly community as well
I am trying to display two x-axis with common Y-axis on plotly for R. I was able to do so as well but starting point for each x-axis is separated from each other Whereas I wish them to be represented a common y-axis.
f1 <- list(
family = "Arial, sans-serif",
size = 18,
color = "grey"
)
f2 <- list(
family = "Old Standard TT, serif",
size = 14,
color = "#4477AA"
)
# bottom x-axis
ax <- list(
title = "Number of PBIs",
titlefont = f1,
anchor = "y",
side = "bottom",
showticklabels = TRUE,
tickangle = 0,
tickfont = f2
)
# top x-axis
ax2 <- list(
title = " ",
overlaying = "x",
anchor = "y",
side = "top",
titlefont = f1,
showticklabels = TRUE,
tickangle = 0,
tickfont = f2
)
# common y-axis
ay <- list(
title = "Process & Sub-Process Areas",
titlefont = f1,
showticklabels = TRUE,
tickangle = 0,
tickfont = f2
)
plot_ly(data = scrum %>%
group_by(Master.Project) %>%
summarise(Total_PBIs_Planned=sum(PBIs.Planned.in.Sprint, na.rm = TRUE),
Total_PBIs_Delivered = sum(Actual.PBI.Delivery,na.rm = TRUE)) %>% inner_join(scrum %>% count(Master.Project)), # creating the desired data frame
color = I("#149EF7")) %>%
# for bottom x-axis
add_segments(x = ~Total_PBIs_Planned, xend = ~Total_PBIs_Delivered,
y = ~Master.Project, yend = ~Master.Project, showlegend = FALSE) %>%
add_trace(x = ~Total_PBIs_Planned, y = ~Master.Project,
name = "Total_PBIs_Planned", type = 'scatter',mode = "markers",
marker = list(color = "#149EF7", size = 15,
line = list(color = '#FFFFFF', width = 1))) %>%
add_trace(x = ~Total_PBIs_Delivered, y = ~Master.Project,
name = "Total_PBIs_Delivered",type = 'scatter',mode = "markers",
marker = list(symbol ="circle-dot",color = "#F71430", size = 10,
line = list(color = '#FFFFFF', width = 1))) %>%
# for top x-axis
add_trace(x = ~n, y = ~Master.Project, xaxis = "x2",
name = "No._of_Sub_projects",type = 'bar',
marker = list(color = "#149EF7"),
opacity = 0.1,
hoverinfo = 'text',
text = ~paste(
Master.Project,
'<br> Total Sub Projects: ',n,
'<br> PBIs Planned: ',Total_PBIs_Planned,
'<br> PBIs Delivered: ',Total_PBIs_Delivered
)
) %>%
plotly::layout(
title = "Product Backlog Items - Planned Vs Delivered", titlefont = f1,
xaxis = ax,
yaxis = ay,
xaxis2 = ax2,
margin = list(l = 250)
)
You want to use rangemode = "tozero" in your axis layout.
ax <- list(
title = "Number of PBIs",
titlefont = f1,
anchor = "y",
side = "bottom",
showticklabels = TRUE,
tickangle = 0,
tickfont = f2,
rangemode = 'tozero')
Or you can specify your specific ranges using
range = c(0, 5)
See Plotly help here: https://plot.ly/r/axes/#rangemode
This question already has an answer here:
Missing data when Supplying a Dual-axis--Multiple-traces to subplot
(1 answer)
Closed 3 years ago.
I have done a multiple subplot. Here is my code:
m <- list(
#l = 50,
# r = 50,
b = 200,
#t = 100,
pad = 0
)
ma <- list(
#l = 100,
b = 100,
# t = 200,
pad = 0
)
p5<-plot_ly(moving_avg,x=~Time,y=~Moving_Average3,type="scatter" ,mode="lines",name ="Memory")%>%
add_trace(x=~Time,y = ~Moving_Average_Cpu3,mode="lines",yaxis = "y2",name ="CPU")%>%
layout(yaxis2 = list(overlaying = "y", side = "right"))%>%
layout(
title = "CPU VS MEMORY(MOVING AVERAGE)",
xaxis = list(
title = "",
zeroline=TRUE),
yaxis = list(
title = "MEMORY(%)",
zeroline=TRUE),
yaxis2 = list(
title = "CPU(%)",
zeroline=TRUE)
)%>%
layout(margin=m)
p6<-plot_ly(moving_avg,x=~Time,y=~Moving_Average3,type="scatter" ,mode="lines",name ="Memory")%>%
add_trace(x=~Time,y = ~Moving_Average_Cpu3,mode="lines",yaxis = "y2",name ="CPU")%>%
layout(yaxis2 = list(overlaying = "y", side = "right"))%>%
layout(
title = "CPU VS MEMORY(MOVING AVERAGE)",
xaxis = list(
title = "",
zeroline=TRUE),
yaxis = list(
title = "MEMORY(%)",
zeroline=TRUE),
yaxis2 = list(
title = "CPU(%)",
zeroline=TRUE)
)%>%
layout(margin=m)
subplot(p5,p6,nrows=1,which_layout = "merge", margin = 0.07,titleX = F, titleY = TRUE)%>%
layout(margin=ma)
I got the following plot:
But as you can all see in the image,in the graphs of both the plots the y axis 2 is getting hidden.
How can i resolve this issue?
Without having the dataset to test it exactly, you want to follow msummersgill's advice over at https://github.com/ropensci/plotly/issues/954
The long and short of it:
Under P6's layout, you should specify (instead of yaxis2=):
yaxis4 = list(
title = "CPU(%)",
zeroline=TRUE,overlaying="y3")
I would like to mark the outlier that appears on my chart writing where it is. Is this possible with plotly?
The code of my graph is here:
library(plotly)
set.seed(1234)
plot_ly(y = rnorm(50), type = 'box') %>%
add_trace(y = rnorm(50, 1)) %>%
layout(title = 'Box Plot',
xaxis = list(title = "cond", showgrid = F),
yaxis = list(title = "rating"))
It's not clear what you tried and what's not working, but one way to identify outliers is to use boxplot.stats() and then you can use that information to add annotations.
library(plotly)
set.seed(1234)
d <- rnorm(50)
d2 <- rnorm(50, 1)
plot_ly(y = d, type = 'box') %>%
add_trace(y = d2) %>%
layout(title = 'Box Plot',
xaxis = list(title = "cond", showgrid = F),
yaxis = list(title = "rating"),
annotations = list(
x = -0.01,
# use boxplot.stats() to get the outlier's y coordinate
y = boxplot.stats(d)$out,
text = "Outlier",
showarrow = FALSE,
xanchor = "right"
)
)