Gauge gradient echarts4r in R - r

I'm needing to create a gauge graph in r, but I can't make the "axisLine" function in gradient colors.
library(echarts4r)
gauge_x <- e_charts() %>%
e_gauge(800,
"Incerteza TC",
startAngle = 180,
endAngle = 0,
min = 0,
max = 1000,
splitNumber = 5,
radius = "185",
itemStyle = list(color = "#000000"),
#axisLine = list(lineStyle = list(color = list(type = "radial", x = "0.5", y = "0.5", r = "0.5",
#backgroundColor = radial_gradient))),
axisTick = list(lineStyle = list(width = 2, color = "#000000")),
splitLine = list(lineStyle = list(color = "#000000", type = "solid")),
axisLabel = list(show = TRUE, color = "#000000", fontWeight = "bold", borderRadius = 5),
pointer = list(show = TRUE, icon = "triangle", length = "80%"), itemStyle = list(color = "black"),
detail = list(show = TRUE, color = "#000000"),
title = list(show = TRUE, fontWeight = "bolder"))
print(gauge_x)

As I am seeing in the documentation, you can not specify gradient color or any pattern fill for axisLine color in the gauge chart. You can only specify an array of colors for segments. As per the documentation,
The axis line of gauge chart can be divided to several segments in different colors. The end position and color of each segment can be expressed by an array.
So you can only color the axisLine by specifying a list of vectors containing the percentages and corresponding colors in the following way :
library(echarts4r)
library(magrittr)
e_charts() %>%
e_gauge(
800,
"Incerteza TC",
startAngle = 180,
endAngle = 0,
min = 0,
max = 1000,
splitNumber = 5,
radius = "185",
itemStyle = list(color = "#000000"),
axisLine = list(lineStyle = list(
color = list(c(0.33, "red"), c(0.67, "blue"), c(1, "green"))
)),
axisTick = list(lineStyle = list(width = 2, color = "#000000")),
splitLine = list(lineStyle = list(color = "#000000", type = "solid")),
axisLabel = list(
show = TRUE,
color = "#000000",
fontWeight = "bold",
borderRadius = 5
),
pointer = list(show = TRUE, icon = "triangle", length = "80%"),
itemStyle = list(color = "black"),
detail = list(show = TRUE, color = "#000000"),
title = list(show = TRUE, fontWeight = "bolder")
)
Hope this helps!

Related

How to remove the whitespace surrounding a plotly bullet chart in R

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.

Add_Trace issue in Plotly

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.

How to add percentage mark to value in Gauge meter in R plotly

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

Add hover text or text annotation in plotly gauge chart

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/

Prevent Overlapping of labels in Plotly

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)

Resources