[英]Create a dial plot in R using plotly or ggplot2
I'd like to create a function that takes some inputs based on which it returns a plot object similar to the one shown below:我想创建一个 function ,它接受一些输入,基于这些输入它返回一个 plot object 类似于下图所示:
The two halves are mirror images in terms of dial position and colors but the text in the center and the labels are different.两半在表盘 position 和 colors 方面是镜像,但中间的文字和标签不同。 I've seen some examples but they don't fully cover what I need:
我看过一些例子,但它们并没有完全涵盖我需要的内容:
sample code示例代码
Attempting the top half of the plot first using plotly
:首先使用
plotly
尝试 plot 的上半部分:
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)
This gives an output:这给出了 output:
I can't figure out a way to:我想不出办法:
current_value
to an arrow insteadcurrent_value
的红色条改为箭头Any help is appreciated.任何帮助表示赞赏。
Your first issue with making a gradient can be done with using scale_*_gradient
, which "creates a two colour gradient (low-high), scale_*_gradient2
creates a diverging colour gradient (low-mid-high), scale_*_gradientn
creates a n-colour gradient."您制作渐变的第一个问题可以使用
scale_*_gradient
来完成,它“创建两个颜色渐变(低-高), scale_*_gradient2
创建一个发散的颜色渐变(低-中-高), scale_*_gradientn
创建一个n 色渐变。” Source 资源
With the following example用下面的例子
scale_colour_gradient(
...,
low = "#132B43",
high = "#56B1F7",
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "colour"
)
As for the arrow, there is a geom for that!至于箭头,有一个geom!
geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type = "closed"))
The issue is dealt with in more detail in this relevant question.在这个相关问题中更详细地处理了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.