简体   繁体   English

使用 plotly 或 Z64B86077DA413996627142D3C4616 在 R 中创建表盘 plot

[英]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:我看过一些例子,但它们并没有完全涵盖我需要的内容:

  1. ggplot Donut chart ggplot 甜甜圈图
  2. Dial Position Gauge Chart Plotly R , 表盘 Position 仪表表 Plotly R ,
  3. https://www.r-graph-gallery.com/doughnut-plot.html https://www.r-graph-gallery.com/doughnut-plot.html
  4. Hide labels in plotly donut chart r 在 plotly 圆环图中隐藏标签 r

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:我想不出办法:

  1. Have a continuous color range rather than the step change I have right now有一个连续的颜色范围,而不是我现在的阶跃变化
  2. change the red bar for 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM