I have created the following bullet chart in R
library(plotly)
fig<-plot_ly()
fig <- fig %>%
add_trace(
type = "indicator",
mode = "number+gauge+delta",
value = 35,
delta = list(reference = 100),
domain = list(x = c(0.25, 1), y = c(0.4, 0.6)),
title = list(text = "Avalue"),
gauge = list(shape = "bullet",axis = list(range = list(NULL, 100)),threshold = list(
line = list(color = "black", width= 2), thickness = 0.75,value = 55),steps = list(
list(range = c(0, 25), color = "red"),
list(range = c(25, 50), color = "blue"),
list(range = c(50, 75), color = "green"),
list(range = c(75, 100), color = "grey")),
bar = list(color = "grey")))
How do I remove the whitespace around the chart. And is there a way I can adjust the length of the chart
You should change your values for your domain in the add_trace. I changed the y to FALSE and x to a different range, which results in the following plot:
library(plotly)
fig<-plot_ly()
fig <- fig %>%
add_trace(
type = "indicator",
mode = "number+gauge+delta",
value = 35,
delta = list(reference = 100),
domain = list(x = c(0.1, 1), y = FALSE),
title = list(text = "Avalue"),
gauge = list(shape = "bullet",axis = list(range = list(NULL, 100)),threshold = list(
line = list(color = "black", width= 2), thickness = 0.75,value = 55),steps = list(
list(range = c(0, 25), color = "red"),
list(range = c(25, 50), color = "blue"),
list(range = c(50, 75), color = "green"),
list(range = c(75, 100), color = "grey")),
bar = list(color = "grey")))
fig
Output:
As you can see, the whitespace is way less.
Related
output$col3<-renderPlotly({
fig <- plot_ly(type = "indicator",mode = "gauge+number",value=VTE_cal(),height = 265,
gauge = list(axis = list(range = list(NULL,100),tickwidth = 1, tickcolor = "black"),
bar = list(color = "darkblue"),bgcolor = "white",borderwidth = 2,bordercolor = "gray",
steps = list(list(range = c(0,20), color = "red"),list(range = c(20,40), color = "pink"),list(range = c(40,60), color = "yellow"),list(range = c(60,80), color = "gold"),list(range = c(80,100), color = "teal")),
threshold = list(line = list(color = "black", width = 4),thickness = 0.75,value = VTE_cal())))
fig <- fig %>%layout(margin = list(l=30,r=50),paper_bgcolor = "lavender",font = list(color = "black", family = "Arial"))
# annotations=list(showarrow=T,arrowhead=3,arrowsize=1,arrowwidth=4))
fig
})
You can use the number argument to specify a prefix or suffix to value; here is an example (with proper indentation!)
plot_ly(
type = "indicator",
mode = "gauge+number",
value = 86,
number = list(suffix = "%"), # <- Specify `prefix` or `suffix`
gauge = list(
axis = list(
range = list(NULL,100),
tickwidth = 1,
tickcolor = "black"),
bar = list(color = "darkblue"),
bgcolor = "white",
borderwidth = 2,
bordercolor = "gray",
steps = list(
list(range = c(0,20), color = "red"),
list(range = c(20,40), color = "pink"),
list(range = c(40,60), color = "yellow"),
list(range = c(60,80), color = "gold"),
list(range = c(80,100), color = "teal")),
threshold = list(
line = list(color = "black", width = 4),
thickness = 0.75,
value = 86))) %>%
layout(
margin = list(l = 30, r = 50),
paper_bgcolor = "lavender",
font = list(color = "black", family = "Arial"))
Can we add hovertext or text annotation in a plotly gauge chart? For example in my plot below I want to add in the green area hover or text or both "Uptake first dose%: 19.8" and in the gray area "Not vaccinated (%):80.2"
library(plotly)
fig <- plot_ly(
type = "indicator",
mode = "gauge+number+delta",
value = 19.8,
title = list(text = "Uptake first dose %", font = list(size = 24)),
delta = list(reference = 70, increasing = list(color = "gray")),
gauge = list(
axis = list(range = list(NULL, 100), tickwidth = 1, tickcolor = "lightgreen"),
bar = list(color = "lightgreen"),
bgcolor = "white",
borderwidth = 2,
bordercolor = "gray",
steps = list(
list(range = c(0, 50), color = "lightgreen"),
list(range = c(20, 100), color = "gray")),
threshold = list(
line = list(color = "black", width = 4),
thickness = 0.75,
value = 70)))
fig <- fig %>%
layout(
margin = list(l=20,r=30),
paper_bgcolor = "lavender",
font = list(color = "darkblue", family = "Arial"))
fig
You can add annotations but I couldn't find a way to get the hovertext without an annotation.
fig <- plotly::plot_ly(
type = "indicator",
mode = "gauge+number+delta",
value = 19.8,
title = list(text = "Uptake first dose %", font = list(size = 24)),
delta = list(reference = 70, increasing = list(color = "gray")),
gauge = list(
axis = list(range = list(NULL, 100), tickwidth = 1, tickcolor = "lightgreen"),
bar = list(color = "lightgreen"),
bgcolor = "white",
borderwidth = 2,
bordercolor = "gray",
steps = list(
list(range = c(0, 50), color = "lightgreen"),
list(range = c(20, 100), color = "gray")),
threshold = list(
line = list(color = "black", width = 4),
thickness = 0.75,
value = 70)))
fig <- fig %>%
plotly::layout(
margin = list(l=20,r=30),
paper_bgcolor = "lavender",
font = list(color = "darkblue", family = "Arial"),
annotations = list(x = 0.05, y = 0.3, text = "19.8%",
hovertext = "Your Text",
showarrow = FALSE))
fig
You could also use add_annotations if you want to add multiples.
See https://plotly.com/r/text-and-annotations/
I have a magneta colored graph made in plotly with certain levels which I have added via vertical lines. Currently I have color filled these areas between the vertical lines.
However I would like to only have a color between the vertical line and the magneta graph (I have circled the aereas as a reference.
Is this possible in plotly?
plot <- plot %>% add_lines(type = 'scatter', mode = "lines",
name = "test", yaxis = 'y2',
x = ~t, y = ~v.x,
line = list(color = '#CC79A7'),
hoverinfo = "text",
text = ~paste(round(v.x, 1), t))
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t),
y = round(quantNUPL[1], 1), yend = round(quantNUPL[1], 1), yaxis = 'y2',
line= list(color = "#0072B0",
dash = "dash",
width = 1),
showlegend = F)
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t),
y = round(quantNUPL[2], 1), yend = round(quantNUPL[2], 1), yaxis = 'y2',
line= list(color = "#0072B0",
dash = "dash",
width = 1),
fill = 'tonexty',
fillcolor = '#56B4E940',
name = "90% UPSIDE")
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t),
y = round(quantNUPL[6], 1), yend = round(quantNUPL[6], 1), yaxis = 'y2',
line= list(color = "#999999",
dash = "dash",
width = 1),
name = "50% UPSIDE")
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t),
y = round(quantNUPL[9], 1), yend = round(quantNUPL[9], 1), yaxis = 'y2',
line= list(color = "#E69F00",
dash = "dot",
width = 1),
name = "20% UPSIDE",
showlegend = F)
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t),
y = round(quantNUPL[10], 1), yend = round(quantNUPL[10], 1), yaxis = 'y2',
line= list(color = "#E69F00",
dash = "dash",
width = 1),
fill = 'tonexty',
fillcolor = "#E69F0020",
name = "20% UPSIDE")
plot <- plot %>% add_segments(x = ~min(t), xend = ~max(t),
y = round(quantNUPL[11], 1), yend = round(quantNUPL[11], 1), yaxis = 'y2',
line= list(color = "#E69F00",
dash = "solid",
width = 1),
fill = 'tonexty',
fillcolor = "#E69F0040",
name = "10% UPSIDE")
You can try setting a trace and defining the max y-values with an ifelse-function.
Example Data
df <- data.frame(x = 1:50,
y = c(20:5,6:10,9:3,4:20, 19:15))
Code
plot_ly(df) %>%
add_lines(type = 'scatter', mode = "lines",
name = "test", yaxis = 'y2',
x = ~x, y = ~y,
line = list(color = '#CC79A7'),
hoverinfo = "text",
text = ~paste(round(y, 1), x)) %>%
#color space between y = 8 and line
add_trace(x = ~x, y = ~ifelse(y <=8, y, 8),
type = "scatter", fill = "toself",
fillcolor = "blue", mode = "none",
yaxis = 'y2') %>%
#background boxes
layout(shapes = list(
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = ~min(x), x1 = ~max(x),
y0 = 0, y1 = 8, yref = "y2"),
list(type = "rect",
fillcolor = "orange", line = list(color = "orange"), opacity = 0.3,
x0 = ~min(x), x1 = ~max(x),
y0 = 18, y1 = 20, yref = "y2"),
list(type = "rect",
fillcolor = "yellow", line = list(color = "yellow"), opacity = 0.3,
x0 = ~min(x), x1 = ~max(x),
y0 = 16, y1 = 20, yref = "y2")))
Plot
Second idea
df <- data.frame(x = 1:50,
y = c(20:5,6:10,9:3,4:8,1,10:20, 5:1))
plot_ly(df) %>%
# add darkblue background
add_trace(x = ~x, y = 8,
type = "scatter", fill = "tozeroy",
fillcolor = "blue", mode = "none",
yaxis = 'y2', hoverinfo = "none") %>%
# set area underneath red line to white
add_lines(type = 'scatter', mode = "lines",
yaxis = 'y2',
x = ~x, y = ~y,
line = list(width = 0),
hoverinfo = "none",
fillcolor = "white",
fill = "tozeroy",
showlegend = FALSE) %>%
# add red line graph
add_lines(type = 'scatter', mode = "lines",
name = "test", yaxis = 'y2',
x = ~x, y = ~y,
line = list(color = '#CC79A7'),
hoverinfo = "text",
text = ~paste(round(y, 1), x)) %>%
layout(shapes = list(
# add lightblue background
list(type = "rect",
fillcolor = "blue", line = list(color = "blue"), opacity = 0.3,
x0 = ~min(x), x1 = ~max(x),
y0 = 0, y1 = 8, yref = "y2"),
# add orange background
list(type = "rect",
fillcolor = "orange", line = list(color = "orange"), opacity = 0.3,
x0 = ~min(x), x1 = ~max(x),
y0 = 18, y1 = 20, yref = "y2"),
#add yellow backround
list(type = "rect",
fillcolor = "yellow", line = list(color = "yellow"), opacity = 0.3,
x0 = ~min(x), x1 = ~max(x),
y0 = 16, y1 = 20, yref = "y2")),
#remove grid
xaxis = list(showgrid = F),
yaxis2 = list(showgrid = F)
)
I am using plotly package to make a heatmap. However, the tooltips show "x,y,z" instead of "Base, weekday, count". Please guide me on how to fix this issue as I'm new to plotly package. My code is:
p<-plot_ly(x=colnames(df_base_dayofweek_m), y=rownames(df_base_dayofweek_m), z = df_base_dayofweek_m, type = "heatmap") %>%
layout(margin = list(l=120))
p <- p %>% layout(title = 'Ride count for different bases',margin=m,
titlefont = list(size = 38, color = "black", family = "Calibri"),
xaxis = list(title = 'Bases',tickfont = list(size = 15), ticktext = sprintf("<b>%s</b>",
levels(factor(df_base_dayofweek_label$Base))),
tickvals = levels(factor(df_base_dayofweek_label$Base)),
titlefont = list(color = "black",size = 28, family = "Arial")),
yaxis = list(title = 'Weekday',tickfont = list(size = 15),ticktext = sprintf("<b>%s</b>",
levels(factor(df_base_dayofweek_label$dayofweek))),
tickvals = levels(factor(df_base_dayofweek_label$dayofweek)),
titlefont = list(color = "black",size = 28,family = "Arial")))
p
Thanks!
As you haven't provided any data I'm taking an example from here.
You can use plot_lys argument hovertemplate to modify the hoverinfo:
library(plotly)
fig <- plot_ly(z = volcano,
type = "heatmap",
hovertemplate = "Base: %{x}<br>Weekday: %{y}<br>Count: %{z}<extra></extra>")
fig
I am plotting bubble chart using Plotly in R. But when I give labels using add_annotations labels overlap and its very difficult to read them. how to prevent this overlapping in plotly. I have tried "geom_label_repel" but its not working with plotly as it gives error.
My Code is
plot_ly(bubbleChartData, x = ~Category1, y = ~Category2, type = 'scatter', mode = 'markers', size = ~BubbleSize,
color = ~Category3, colors = brewer.pal(8,"Dark2"),
marker = list( sizemode = 'diameter'), text=~Category4) %>%
add_annotations( showarrow=F,xanchor="right",xref = "x",ax = 20,
ay = -40,
yref = "y",x = ~Category1, y = ~Category2, text=~Category4,font = list(size = 10))%>%
layout(title = '',font=list(size=10),
xaxis = list(
title = "",
showgrid=FALSE,
showline=FALSE,
tickfont = list(
size = 16,
color = 'rgb(0,0,0)'),
titlefont = list(
size = 18,
color = 'rgb(0,0,0)')),
yaxis = list(
title = "",
showgrid=FALSE,
showline=FALSE,
titlefont = list(
size = 18,
color = 'rgb(0,0,0)'),
tickfont = list(
size = 16,
color = 'rgb(0,0,0)')),
showlegend = F)