如何在 R 中编辑绘图刻度线?

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

我无法删除绘图仪表图表上的刻度并将开始(最小)和结束(最大)刻度置于图表下方。

这是我用来创建仪表的代码。

plot1 <- plotly::plot_ly(
  type = "indicator",
  mode = "gauge+number",
  value = 42.2,
  title = list(text = "Jump Height", font = list(size = 30, color = "white")),
  gauge = list(
    axis = list(range = list(NULL, 50), tickfont = list(color = "black"), tickcolor = "black", ticks = ""),
    bar = list(color = "royalblue", thickness = 1),
    bgcolor = "white",
    borderwidth = 0))
plot1 <- plot1 %>% plotly::layout(
  margin = list(t=20,r=20,b=20,l=20),
  paper_bgcolor = "black",
  font = list(color = "white", family = "Arial"))

plot1

正如您所看到的,图表中存在刻度,但我刚刚调整了颜色,以便它们融入背景并且不显示。我什至尝试过使用tickvals、ticktext、tickmode 和tickangle 来获取我想要的标签,但随后我的标题就变得一团糟。

我希望我的仪表类似于这样。

如果有人知道 ggplot2 中的替代方案,那就太好了!

r ggplot2 plotly
1个回答
0
投票

这个函数将生成一个 ggplot 来完成这个任务。只需给它您想要表示的数字和仪表上限即可。您也可以指定仪表的颜色和标题:

make_gauge <- function(x, n, color =  "#ef8d49", title = NULL) {
  data.frame(x = c(5 * cos(seq(-pi, 0, len = 100)), 
                   3 * cos(seq(0, -pi, len = 100)),
                   5 * cos(seq(-pi, -pi + pi * x/n, len = 100)),
                   3 * cos(seq( -pi + pi * x/n, -pi, len = 100))),
             y = c(5 * -sin(seq(-pi, 0, len = 100)), 
                   3 * -sin(seq(0, -pi, len = 100)),
                   5 * -sin(seq(-pi, -pi + pi * x/n, len = 100)),
                   3 * -sin(seq( -pi + pi * x/n, -pi, len = 100))),
             group = rep(c("off", "on"), each = 200)) |>
    ggplot(aes(x, y, fill = group)) +
    geom_polygon() +
    scale_fill_manual(values = c("#eceaea", color), guide = "none") +
    annotate("text", x = c(-4, 0, 4), y = c(-0.5, 0.6, -0.5),
             label = c(0, round(42.2, 1), 50), size = c(12, 30, 12),
             colour = c("#c8c8c8", "#808080", "#c8c8c8"), 
             fontface = 2) +
    ggtitle(title) +
    coord_equal() +
    theme_void() +
    theme(plot.title = element_text(hjust = 0.5, size = 40, color = "#808080",
                                    vjust = 2, face = 2))
}

测试,我们有:

make_gauge(42.2, 50, title = "Jump Height")

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