Why Multiple y axes subplots in Plotly R are being hidden? [duplicate] - r

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")

Related

Hover is not spot on in R plotly with hovermode 'x unified'

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.

Showing full categorical x-axis even when there are no points to plot

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

Two X-axis in Plotly for R

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

How to add textbox on plot in plotly R?

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))

add vertical and horizontal lines in hplot (rcharts)

I'm trying to add horizontal and vertical lines in a highchart (rcharts) in a revealjs presentation.
I tried to modify the code of this post in this way:
require(xlsx)
library(rCharts)
Perhplot.df <-read.xlsx("C:\\RDirectory\\AREALAVORO\\JOB\\RISULTATI2.xlsx", sheetName="completo2")
lDf <- split(Perhplot.df, Perhplot.df$variable)
h16 <- hPlot(protection ~ days, data = lDf$Exposure,
type = "bubble",
group = "label",
title = "By Days of Exposure",
subtitle = "Move the mouse pointer on the bubbles to view the data",
size = "cluster_size",
group = "label")
h16$set(width = 1000, height = 600)
ord <- c("Less than 1 week"=0,
"1-2 weeks"=1,
"3-4 weeks"=2,
"More than 4 weeks"=3,
"Mean"=4
)
h16$params$series <- lapply(h16$params$series, function(d){
temp = ord[d$name]
names(temp) = NULL
d$legendIndex = temp
return(d)
})
h16$yAxis(min = 35, max = 70, title = list(text = "Level of Protection"))
h16$xAxis(min = 0, max = 45, title = list(text = "Days of Exposure"))
dfy<-data.frame(y=c(35,58,70), x=c(18.8,18.8,18.8))
h16$layer(y~x,data=dfy,type="line",color=list(const = 'darkblue'))
h16$show('inline', include_assets = TRUE)
the bubble plot is ok but then I try to add the vertical line I have this error:
Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : ‘layer’ is not a valid field or method name for reference class “Highcharts”
So the solution works for Dimple Charts but not for Highcharts...
As same as rcharts highcharts plotLines.
You need to use plotLines argument:
library("rCharts")
# Some data
x <- abs(rnorm(10))
# A graph with column
h <- Highcharts$new()
h$chart(type = "column")
h$series(data = x)
h$xAxis(categories = letters[1:10])
# the horizontal line
h$yAxis(title = list(text = "rnorm()"),
plotLines = list(list(
value = mean(x),
color = '#ff0000',
width = 3,
zIndex = 4,
label = list(text = "mean",
style = list( color = '#ff0000', fontWeight = 'bold' )
))))
h
Yo add vertical you change yAxis by xAxis.
Or if you use highcharter (It's a new wrapper of highcharts for R):
h2 <- highchart() %>%
hc_chart(type = "column") %>%
hc_add_serie(data = x) %>%
hc_xAxis(categories = letters[1:10]) %>%
hc_yAxis(title = list(text = "rnorm()"),
plotLines = list(list(
value = mean(x),
color = '#ff0000',
width = 3,
zIndex = 4,
label = list(text = "mean",
style = list( color = '#ff0000', fontWeight = 'bold' )
))))
h2
Source: http://jkunst.com/highcharter/highcharts-api.html#hc_xaxis-and-hc_yaxis

Resources