Related
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
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:
I am trying to make a contour plot and draw a line on top of it (which I can do). I then overlaid two other lines using add_trace. For some reason the two lines I add using add_trace comes out orange instead of grey even though I specify grey (line = list(color = 'grey', )
I don't know how to add my data, it is very big. Is there any obvious reason as to why the colour changes to orange? If I change the width or the dash, it works. it just doesn't want to use the grey colour!
Thank you
(plot <- plot_ly(df, x = ~A, y = ~B, z = ~Difference, zauto = FALSE, zmin = -250, zmax = 250,
type="contour",
colorbar = list(title = "", titleside='right',
tickvals=c(-250, -200, -150, -100, -50, 0, 50, 100, 150, 200, 250), len = 1),
colorscale = "RdBu",
contours = list(start = 0, end = 0, coloring='heatmap', coloring='lines'),
line = list(color = 'black', width = 2)) %>%
add_trace(z = df$C, showscale = FALSE, line = list(color = 'grey', width = 2, dash = 'dash'), contours = list(start = 0, end = 0, coloring='lines')) %>%
add_trace(z = df$C, showscale = FALSE, line = list(color = 'grey', width = 2, dash = 'solid'), contours = list(start = 0, end = 0, coloring='lines')) %>%
layout(margin = list(l = 50, r = 70, b = 50, t = 50, pad = 4),
title = "", xaxis = x, yaxis = list(title = ""), font=t))
I guess the problem in your above code is, that you aren't specifiying the trace type. Accordingly plot_ly assumes that you are adding two more contour traces. Those traces are inheriting the colorscale you defined.
To avoid this you need to specify type = "scatter", mode = "lines", inherit = FALSE.
I made a simple example based on this.
library(plotly)
fig <- plot_ly(
x = c(-9, -6, -5, -3, -1),
y = c(0, 1, 4, 5, 7),
z = matrix(c(10, 10.625, 12.5, 15.625, 20, 5.625, 6.25, 8.125, 11.25, 15.625, 2.5, 3.125, 5, 8.125, 12.5, 0.625, 1.25, 3.125,
6.25, 10.625, 0, 0.625, 2.5, 5.625, 10), nrow = 5, ncol = 5),
type = "contour", colorbar = list(title = "", titleside='right'),
colorscale = "RdBu",
line = list(color = 'black', width = 2)) %>%
add_trace(x = -1:-7, y = 1:7, type = "scatter", mode = "lines", line = list(color = 'lightgreen', width = 2, dash = 'solid'), inherit = FALSE)
fig
I am stuck with the following plotly example which I would like to exploit:
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~uempmed, name = "unemployment")
# add shapes to the layout
p <- layout(p, title = 'Highlighting with Rectangles',
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = "1980-01-01", x1 = "1985-01-01", xref = "x",
y0 = 4, y1 = 12.5, yref = "y"),
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
y0 = 4, y1 = 12.5, yref = "y")))
How can I include hover information for those rectangles? Neither of the following works:
layout(p, title = 'Highlighting with Rectangles',
shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = "1980-01-01", x1 = "1985-01-01", xref = "x",
# text - hoverinfo pair
text = "hello", hoverinfo = "text",
y0 = 4, y1 = 12.5, yref = "y"),
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.2,
x0 = "2000-01-01", x1 = "2005-01-01", xref = "x",
# hovertext
hovertext = "good bye",
y0 = 4, y1 = 12.5, yref = "y")))
Is there any way how I can plot rectangles in plotly and apply a hover to them. Unfortunately, at least as I am aware of, there is no type = "rect" or anything like that available in R.
You can do it by adding polygons instead of shapes. Look below;
library(plotly)
plot_ly() %>%
add_polygons(x=c(as.Date("1980-01-01"), as.Date("1980-01-01"),
as.Date("1985-01-01"), as.Date("1985-01-01")),
y=c(4, 12.5, 12.5, 4),
line=list(width=0),
fillcolor='rgba(255, 212, 96, 0.5)', inherit = FALSE,
name = 'Hello') %>%
add_polygons(x=c(as.Date("2000-01-01"), as.Date("2000-01-01"),
as.Date("2005-01-01"), as.Date("2005-01-01")),
y=c(4, 12.5, 12.5, 4),
line=list(width=0),
fillcolor='rgba(255, 212, 96, 0.5)', inherit = FALSE,
name = 'Bye') %>%
add_markers(data = economics, x = ~date, y = ~uempmed, name = "unemployment")
I'm trying to customise the x-axis. I currently have the "Site" variable on the x-axis. The range is from 15 to 24 with site a and b for each number i.e. for 15, there is 15a and b (and so on to 24). When I label all bars, it looks messy and I'm trying to customise it so for example, the number 15 is below but the individual bars are labelled a and b. This way, the individual bars will be identifiable but it won't look so crowded.
Here is my code so far:
#Stacked bar chart exp2
BarChartData2 <- read.csv("Bar chart data exp 2.csv")
Site <- BarChartData2[,4]
Card <- BarChartData2[,2]
Pen <- BarChartData2[,3]
data2 <- data.frame(Site, Card, Pen)
pstacked2 <- plot_ly(data2, x = ~Site, y = ~Card, type = 'bar', name = 'Card', marker = list(color = 'Black')) %>%
add_trace(y = ~Pen, name = 'Pen', marker = list(color = 'red')) %>%
layout(yaxis = list(title = 'Number of Bee Visits'), barmode = 'stack', font = list(family = 'Times New Roman', size =14, color ="black"), xaxis = list(autotick = F, dtick = 2))
pstacked2
Any help/other ideas of how to do this will be much appreciated!
Here is something to help you. You can play with shapes and annotations to get exatly what you want.
plot_ly(data2, x = ~Site, y = ~Card, type = 'bar', name = 'Card', marker = list(color = 'Black')) %>%
add_trace(y = ~Pen, name = 'Pen', marker = list(color = 'red')) %>%
layout(yaxis = list(title = 'Number of Bee Visits'),
barmode = 'stack',
font = list(family = 'Times New Roman', size =14, color ="black"),
xaxis = list(autotick = F, dtick = 2),
margin = list(
r = 10,
t = 25,
b = 100,
l = 110
),
shapes = list(
list(
line = list(
color = "rgba(68, 68, 68, 0.5)",
width = 1
),
type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = -0.05,
y1 = -0.05,
yref = "paper"
)
),
annotations = list(
list(
x = 0.04,
y = -0.1,
showarrow = FALSE,
text = "15",
xref = "paper",
yref = "paper"
),
list(
x = 0.14,
y = -0.1,
showarrow = FALSE,
text = "16",
xref = "paper",
yref = "paper"
),
list(
x = 0.24,
y = -0.1,
showarrow = FALSE,
text = "17",
xref = "paper",
yref = "paper"
),
list(
x = .35,
y = -0.1,
showarrow = FALSE,
text = "18",
xref = "paper",
yref = "paper"
),
list(
x = .45,
y = -0.1,
showarrow = FALSE,
text = "19",
xref = "paper",
yref = "paper"
),
list(
x = .55,
y = -0.1,
showarrow = FALSE,
text = "20",
xref = "paper",
yref = "paper"
),
list(
x = .65,
y = -0.1,
showarrow = FALSE,
text = "21",
xref = "paper",
yref = "paper"
),
list(
x = .75,
y = -0.1,
showarrow = FALSE,
text = "22",
xref = "paper",
yref = "paper"
),
list(
x = .85,
y = -0.1,
showarrow = FALSE,
text = "23",
xref = "paper",
yref = "paper"
),
list(
x = .95,
y = -0.1,
showarrow = FALSE,
text = "24",
xref = "paper",
yref = "paper"
)
)
)
I'm looking for a way to sort my plotly bar plot by yaxis alphabetically. I tried several ways but the order of y is still from z to a, instead of a to z. Please help me out!
qt <- c("A", "C", "B","B","A", "C", "C")
y <- c("q1", "q2", "q3", "q4", "q5", "q6", "q7")
x1 <- c(20, 10, 15, 15, 20, 10, 15)
x2 <- c(10, 20, 20, 10, 10, 30, 10)
x3 <- c(10, 10, 5, 10, 10, 5, 5)
x4 <- c(20, 25, 25, 35, 55, 40, 35)
x5 <- c(40, 35, 35, 30, 5, 15, 35)
df <- data.frame(qt, y, x1, x2, x3, x4, x5)
df$qt <- factor(df$qt, levels = c("A", "B", "C"))
plot_ly(df) %>%
add_trace(x = ~x1, y = ~y, marker = list(color = 'rgb(202,0,32)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x2, y = ~y, marker = list(color = 'rgb(244,165,130)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x3, y = ~y, marker = list(color = 'rgb(223,223,223)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x4, y = ~y, marker = list(color = 'rgb(146,197,222)'), type = 'bar', orientation = 'h') %>%
add_trace(x = ~x5, y = ~y, marker = list(color = 'rgb(5,113,176)'), type = 'bar', orientation = 'h') %>%
layout(title="mytitle",
xaxis = list(title = "",
showticklabels = TRUE,
zeroline = FALSE,
domain = c(0.15, 1)),
yaxis = list(title = "",
showticklabels = FALSE,
zeroline = FALSE),
barmode = 'relative',
paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)',
autosize=T,
margin = list(l = 150, r = 50, t = 100, b = 50),
showlegend=F) %>%
# labeling the y-axis
add_annotations(xref = 'paper', yref = 'y', x = 0.14, y = df$y,
xanchor = 'right',
text = df$y,
font = list(family = 'Arial', size = 15,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE, align = 'right')%>%
#labeling the y-axis (category)
add_annotations(xref = 'paper', yref = 'qt', x = 0.01, y = df$y,
xanchor = 'right',
text = df$qt,
font = list(family = 'Arial', size = 15,
color = 'rgb(67, 67, 67)'),
showarrow = FALSE, align = 'right')
The primary goal I would like to accomplish is to order this by the variable qt (from A to C). But if this is impossible, ordering the plot by y is also desirable (from q1 to q7). My plot looks like this:
Thank you in advance!