gpt4 book ai didi

r - 使用 plotly 或 ggplot2 在 R 中创建拨号图

转载 作者:行者123 更新时间:2023-12-04 16:12:01 26 4
gpt4 key购买 nike

我想创建一个函数,该函数根据一些输入返回一个类似于下图所示的绘图对象:

enter image description here

两半在表盘位置和颜色上是镜像,但中间的文字和标签不同。我看过一些例子,但它们并没有完全涵盖我所需要的:

  • ggplot Donut chart
  • Dial Position Gauge Chart Plotly R ,
  • https://www.r-graph-gallery.com/doughnut-plot.html
  • Hide labels in plotly donut chart 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

    我想不出办法:
  • 有一个连续的颜色范围,而不是我现在的阶跃变化
  • 将红色条更改为 current_value改为箭头

  • 任何帮助表示赞赏。

    最佳答案

    制作渐变的第一个问题可以通过使用来完成scale_*_gradient ,它“创建了一个双色渐变(低-高),scale_*_gradient2 创建了一个发散的颜色渐变(低-中-高),scale_*_gradientn 创建了一个 n 色渐变。” Source

    用下面的例子

    scale_colour_gradient(
    ...,
    low = "#132B43",
    high = "#56B1F7",
    space = "Lab",
    na.value = "grey50",
    guide = "colourbar",
    aesthetics = "colour"
    )

    至于箭头,有一个几何体!
    geom_line(arrow = arrow(length=unit(0.30,"cm"), ends="first", type = "closed"))

    该问题在 this relevant question. 中有更详细的处理。

    关于r - 使用 plotly 或 ggplot2 在 R 中创建拨号图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61484675/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com