如何修复此径向图表上的标签和线条?

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

我正在尝试为我的下一次演示创建一些东西。这是商店里排队的平均等待时间。我正在使用 R 和

ggplot
。这是我正在使用的数据和代码。

## Data
Store = data.frame(Line = c("Line 1","Line 2","Line 3","Line 4","Line 5","Line 6"),
                  Avg_wait_per_min = c(4.2,4.4,6.4,5.7,3.2,5.3))

Duration = c(0,1,2,3,4,5,6)

## Library
library(tidyverse)

## Plotting
ggplot(Store, aes(x = reorder(Line,Avg_wait_per_min),y = Avg_wait_per_min,
       fill = Line)) +
geom_bar(stat = "identity", width = .95) +
geom_text(aes(label = Avg_wait_per_min), y = -0.15, hjust = 0.5, angle=90, size=3,
          color="black",position = "identity") +
geom_hline(yintercept = Duration, color = "grey", size = .75, linetype = "solid") +
geom_text(data = data.frame(y = Duration), aes(x = Inf, y = y,
          label = paste0(Duration, "'")), color = "black", size = 4, hjust = 1,
          inherit.aes = F, fontface = "bold", nudge_x = 0.1) +
labs(x = NULL, y = NULL) +
coord_radial(theta = "y", start = 4.42, direction = 1, clip = "off",
             inner.radius =.5) +
theme_void() +
theme(axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    legend.position = 'bottom',
    legend.text = element_text(size = 7),
    legend.title = element_blank(),
    legend.key.size = unit(.4,'cm')) +
scale_x_discrete() +
scale_y_continuous(limits = c(0,7), breaks = Duration,
                   label = paste0(Duration, "'")) +
scale_fill_viridis(discrete = T)

这是我得到的图表

Radial chart

我到处寻找,询问人工智能,但没有一个按照我想要的方式工作。

基本上我想重新创建这样的图表:

卡拉马尼斯的陌生人歌曲

但更简单。我也一直在看他的代码,但我不明白如何将他的代码实现到我的代码中。有人可以帮助我如何使我的径向图表更整洁吗?
例如:
a) 如何让

Duration
的标签编号整齐地出现在圆外,而不是堆积起来。
b) 如何让灰线一直到达圆心。
等等

提前谢谢您!!

r ggplot2 charts visualization radial
1个回答
0
投票

类似这样的事情。

  • geom_hline
    替换为
    annotate
    ,并带有负值
    x
    inner.radius
    设置为 0。
  • Y 轴标签保持原样,而不是
    geom_text
    。主题更改为最小并删除网格。
  • X 标签具有
    y
    = 0 和
    hjust
    略高于 1 以对齐标签
## Data
Store = data.frame(Line = c("Line 1","Line 2","Line 3","Line 4","Line 5","Line 6"),
                   Avg_wait_per_min = c(4.2,4.4,6.4,5.7,3.2,5.3))

Duration = c(0,1,2,3,4,5,6)

## Library
library(tidyverse)
library(viridis)
#> Loading required package: viridisLite

## Plotting
ggplot(Store, aes(x = reorder(Line,Avg_wait_per_min),y = Avg_wait_per_min,
                  fill = Line)) +
  geom_bar(stat = "identity", width = .95) +
  geom_text(aes(label = Avg_wait_per_min), y = 0, 
            hjust = 1.2, vjust = 0.5, 
            angle = 90, size = 3,
            color="black",position = "identity") +
  annotate("segment", 
           x = -5, xend = length(Duration), 
           y = Duration, yend = Duration, 
           col = "grey", size = .75, linetype = "solid") +
  labs(x = NULL, y = NULL) +
  coord_radial(theta = "y", start = 4.42, direction = 1, clip = "off",
               inner.radius =0) +
  theme_minimal() +
  theme(axis.text.y = element_blank(),
        panel.grid = element_blank(),
        legend.position = 'bottom',
        legend.text = element_text(size = 7),
        legend.title = element_blank(),
        legend.key.size = unit(.4,'cm')) +
  scale_x_discrete() +
  scale_y_continuous(limits = c(0,7), breaks = Duration,
                     label = paste0(Duration, "'")) +
  scale_fill_viridis(discrete = T)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

创建于 2024-12-17,使用 reprex v2.0.2

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