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)
Related
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.
How can I increase the length of the green (lcl) and red line (lsl) so that they extend to the end of the plot (rather than stopping halfway) in the add_trace function?
Here is my code for my graph:
output$p <- renderPlotly({
plot1 %>%
add_trace(y = lcl(),type = "scatter", mode = "lines", line = list(color = 'lightgreen', width = 2, dash = 'solid'), inherit = FALSE, name = paste("LCL =", lcl()[1])) %>%
add_trace(y = lsl(),type = "scatter", mode = "lines", line = list(color = 'red', width = 2, dash = 'solid'), inherit = FALSE, name = paste("LSL =", lsl()[1])) %>%
%>%
layout(
title = paste(" cell culture", input$select),
yaxis = list( showline = TRUE,
mirror = "ticks",
linecolor = toRGB("grey"),
linewidth = 1.85
),
xaxis = list( showline = TRUE,
mirror = "ticks",
linecolor = toRGB("grey"),
linewidth = 1.85,
tickangle=70))
})
edit: i got rid of the code for the actual plot (the scatter plot) for simplicity, that graph is fine and does not need any tweaking.
Instead of using add_trace for creating horiontal lines that span the entire graph I would add horizontal lines as a shapes to the layout:
layout(shapes = list(
list(
type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 75,
y1 = 75,
line = list(color = 'lightgreen', width = 2)
),
list(
type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 100,
y1 = 100,
line = list(color = 'red', width = 2)
)
))
xref = "paper" is used so the line spans the entire graph.
Positioning in paper coordinates is not done in absolute pixel terms, but rather in terms relative to a coordinate system defined with an origin (0,0) at (layout.margin.l, layout.margin.b) and a point (1,1) at (layout.width-layout.margin.r, layout.height-layout.margin.t)...
https://plotly.com/python/figure-structure/#positioning-with-paper-container-coordinates-or-axis-domain-coordinates
Change the y values according to your requirements.
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 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