使用 plotly 或 ggplot2 在 R 中创建一个表盘图。

问题描述 投票:1回答:1

我想创建一个函数,接受一些输入,并根据这些输入返回一个类似于下图的绘图对象。

enter image description here

两部分在刻度盘的位置和颜色上是镜像的 但中间的文字和标签是不同的。我看过一些例子,但它们并不能完全满足我的需求。

  1. ggplot甜甜圈图
  2. 表盘位置图 绘图式R,
  3. https:/www.r-graph-gallery.comdoughnut-plot.html
  4. 在Plotly甜甜圈图中隐藏标签r

示例代码

先尝试使用上半部分的情节。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)

这给出了一个输出。enter image description here

我想不出一个办法来:

  1. 有一个连续的颜色范围,而不是现在的阶梯式变化。
  2. 换红条 current_value 变成箭头

希望得到任何帮助。

r ggplot2 plotly gauge donut-chart
1个回答
0
投票

你的第一个问题是如何制作渐变,可以用以下方法解决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"))

这个问题在 这个相关问题。

© www.soinside.com 2019 - 2024. All rights reserved.