I have got this code that is in R Markdown and uses plotly. I want to add a limit on Y axis. Here it uses the maximum of "est" variables but I want to put a cut off of 10. I understand that this would affect the confidence interval (upper and lower) as well.
I have provided the data using dput()
here. Hope this is the right way as its first time using this function.
dput(plot_data)
structure(list(quarter = structure(c(2014.25, 2015.5, 2015.75,
2016, 2016.25, 2016.5, 2016.75, 2017, 2017.25, 2017.5, 2017.75,
2018, 2018.25, 2018.5, 2018.75, 2019, 2019.25, 2019.5, 2019.75,
2020, 2020.25, 2020.5, 2020.75, 2021, 2021.25, 2021.75), class = "yearqtr"),
cases = c(1L, 1L, 38L, 4L, 2L, 8L, 9L, 13L, 6L, 20L, 32L,
42L, 26L, 18L, 25L, 11L, 5L, 4L, 4L, 3L, 1L, 2L, 2L, 2L,
3L, 1L), est = c(0, 0, 0, 21, 0.153846153846154, 0.238095238095238,
2.83333333333333, 2.2, 1.11764705882353, 1.18181818181818,
2.73684210526316, 2.84615384615385, 1.30769230769231, 0.594594594594595,
0.632352941176471, 0.818181818181818, 0.372093023255814,
0.25, 0.5, 0.777777777777778, 0.5, 0.428571428571429, 1,
1.33333333333333, 1.25, 1), lower = c(0, 0, 0, 9.36875, 0.0605698529411765,
0.0900568181818182, 1.24583333333333, 1.0825, 0.537, 0.704503105590062,
1.88461538461538, 1.96911764705882, 0.914764492753623, 0.402261904761905,
0.390400682011935, 0.539913043478261, 0.224595551061679,
0.103584229390681, 0.213787185354691, 0.25, 0.105277777777778,
0, 0.1825, 0.0950000000000005, 0.2, 0.154166666666667), upper = c(0,
0, 0, 9, 0.287045454545454, 0.448195187165775, 9, 4.98499999999999,
2.49523809523809, 2.1875, 4.5, 4.60504201680672, 1.88531641397495,
0.867572463768115, 0.869867374005305, 1.35281440162272, 0.629361179361179,
0.457125307125307, 1.09090909090909, 2.17285714285714, 3,
2.68333333333333, 9, 9, 7.52499999999999, 6)), row.names = c(NA,
-26L), class = c("tbl_df", "tbl", "data.frame"))
plot_data <- tibble(
quarter = rp_list$infection_quarter,
cases = rp_list$cases,
est = edr_frame$est,
lower = edr_frame$lower,
upper = edr_frame$upper
) %>%
mutate (upper = case_when(upper >= 10 ~9,
upper >= 9 ~ 8,
TRUE ~ upper))
plot_edr <- plot_data %>%
# Dashed line for upper CI
plotly::plot_ly(
name = "95% CI (upper)",
type = 'scatter',
mode = 'lines',
x = ~quarter,
y = ~upper,
line = list(
dash = "dot",
color = c("#A8B9CB")
)
) %>%
add_trace(
name = "95% CI (lower)",
type = 'scatter',
mode = 'lines',
x = ~quarter,
y = ~lower,
line = list(
dash = "dot",
color = c("#A8B9CB")
)
) %>%
add_trace(
name = "EDR",
type = 'scatter',
mode = 'lines',
x = ~quarter,
y = ~est,
line = list(
dash = "line",
color = c("#2F3D70")
)
) %>%
layout(
title = 'Ratio',
xaxis = list(title = "Quarter"),
yaxis = list(title = "Ratio with 95% CI"),
shapes = list(
list(type = "rect",
fillcolor = "green", line = list(color = "green"), opacity = 0.1,
x0 = min(plot_data$quarter), x1 = max(plot_data$quarter), xref = "x",
y0 = 0, y1 = 1, yref = "y"),
list(type = "rect",
fillcolor = "red", line = list(color = "red"), opacity = 0.1,
x0 = min(plot_data$quarter), x1 = max(plot_data$quarter), xref = "x",
y0 = 1, y1 = max(plot_data$upper), yref = "y1")))
Many Thanks
CodePudding user response:
You should add range = c(0,10)
to your yaxis
in your layout
the plot. You can use the following code:
library(plotly)
library(tidyverse)
plot_edr <- plot_data %>%
# Dashed line for upper CI
plotly::plot_ly(
name = "95% CI (upper)",
type = 'scatter',
mode = 'lines',
x = ~quarter,
y = ~upper,
line = list(
dash = "dot",
color = c("#A8B9CB")
)
) %>%
add_trace(
name = "95% CI (lower)",
type = 'scatter',
mode = 'lines',
x = ~quarter,
y = ~lower,
line = list(
dash = "dot",
color = c("#A8B9CB")
)
) %>%
add_trace(
name = "EDR",
type = 'scatter',
mode = 'lines',
x = ~quarter,
y = ~est,
line = list(
dash = "line",
color = c("#2F3D70")
)
) %>%
layout(
title = 'Ratio',
xaxis = list(title = "Quarter"),
yaxis = list(title = "Ratio with 95% CI", range = c(0,10)),
shapes = list(
list(type = "rect",
fillcolor = "green", line = list(color = "green"), opacity = 0.1,
x0 = min(plot_data$quarter), x1 = max(plot_data$quarter), xref = "x",
y0 = 0, y1 = 1, yref = "y"),
list(type = "rect",
fillcolor = "red", line = list(color = "red"), opacity = 0.1,
x0 = min(plot_data$quarter), x1 = max(plot_data$quarter), xref = "x",
y0 = 1, y1 = max(plot_data$upper), yref = "y1")))
Output: