我想创建一个函数,接受一些输入,并根据这些输入返回一个类似于下图的绘图对象。
两部分在刻度盘的位置和颜色上是镜像的 但中间的文字和标签是不同的。我看过一些例子,但它们并不能完全满足我的需求。
示例代码
先尝试使用上半部分的情节。plotly
:
plot_func <- function(current_value){
fig <- plot_ly(
domain = list(x = c(0, 1), y = c(0, 1)),
value = current_value,
title = list(text = "Rating"),
type = "indicator",
mode = "number+gauge",
gauge = list(
axis =list(range = list(100, 85)),
bar = list(
# color = 'white',
# line = list(width = 1),
thickness = 0
),
steps = list(
list(range = c(85,90), color = "#b20000", name = 'E'),
list(range = c(90,92.5), color = "#e09999", name = 'D'),
list(range = c(92.5, 95), color = "#ffffb2", name = 'C'),
list(range = c(95, 97.5), color = '#7fbf7f', name = 'B'),
list(range = c(97.5,100), color = "#008000", name = 'A')),
threshold = list(
line = list(color = "red", width = 4),
thickness = 0.75,
value = current_value)))
return(fig)
}
myplot <- plot_func(current_value = 99.8)
我想不出一个办法来:
current_value
变成箭头希望得到任何帮助。
你的第一个问题是如何制作渐变,可以用以下方法解决scale_*_gradient
它 "创建了一个双色渐变(低-高)。scale_*_gradient2
创建一个分歧的颜色渐变(低-中-高)。scale_*_gradientn
创建一个n色渐变。" 来源
以下是一个例子
scale_colour_gradient(
...,
low = "#132B43",
high = "#56B1F7",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "colour"
)
至于箭头,有一个geom的问题!
geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type = "closed"))
这个问题在 这个相关问题。