使用连续和离散刻度填充美学两次

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

我有一个如下数据:

structure(list(bucket = structure(1:23, .Label = c("(1.23,6.1]", 
"(6.1,10.9]", "(10.9,15.6]", "(15.6,20.4]", "(20.4,25.1]", "(25.1,29.9]", 
"(29.9,34.6]", "(34.6,39.4]", "(39.4,44.2]", "(44.2,48.9]", "(48.9,53.7]", 
"(53.7,58.4]", "(58.4,63.2]", "(63.2,68]", "(68,72.7]", "(72.7,77.5]", 
"(77.5,82.2]", "(82.2,87]", "(87,91.7]", "(91.7,96.5]", "(96.5,101]", 
"(101,106]", "(106,111]"), class = "factor"), value = c(0.996156321090158, 0.968144290236367, 0.882793110384066, 0.719390676388129, 0.497759597498133, 
0.311721580067415, 0.181244079443301, 0.0988516758834657, 0.0527504526341006, 
0.0278716018561911, 0.0145107725175315, 0.00785033086321829, 
0.00405759957072942, 0.00213190168252939, 0.00109610249274952, 
0.000578154695264754, 0.000301095727545301, 0.000155696457494707, 
8.2897211122996e-05, 4.09225082176349e-05, 2.33782236798641e-05, 
1.21665352966827e-05, 6.87373003802479e-06), bucket_id = 1:23), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -23L))

我希望将其视为圆形堆积条形图:

cutoff_values <- seq(0, 115, by = 5)


library(tidyverse)
ex %>% 
  mutate(r0 = cutoff_values[-length(cutoff_values)],
         r = cutoff_values[-1]) %>% 
  mutate(x0 = 100, 
         y0 = 50) %>% 
  ggplot(aes(x0 = x0, y0 = y0, r0 = r0, r = r)) +
  ggforce::geom_arc_bar(aes(start = 0, end = 2 * pi, fill = value),
                        colour = NA) +
  theme_void() +
  labs(fill = 'colour')

enter image description here

但是我还需要能够用不同的填充标记出一些特定的桶。所以我需要能够使用连续比例的value来保存填充,但也要用另一种颜色填充一个特定的层(让我们说bucket == 15),留下其他层(桶)。可能吗?标记第15桶的替代方案是什么?

r ggplot2 ggforce
1个回答
1
投票

我相信这可以通过relayer包来完成,它仍然是高度实验性的。您可以在单独的geom中复制数据的子集,并为其提供另一种填充美学。这个单独的geom然后可以用管道传输到rename_geom_aes(),你必须为你重新命名的美学设置scale_fill_*()。你可能会得到一个警告,即geom忽略了未知的美学,但我不知道这是否有帮助。

以下是使铲斗15变红的示例。

library(tidyverse)
library(relayer) # https://github.com/clauswilke/relayer

ex <- df %>% 
  mutate(r0 = cutoff_values[-length(cutoff_values)],
         r = cutoff_values[-1]) %>% 
  mutate(x0 = 100, 
         y0 = 50)

ggplot(ex, aes(x0 = x0, y0 = y0, r0 = r0, r = r)) +
  ggforce::geom_arc_bar(aes(start = 0, end = 2 * pi, fill = value),
                        colour = NA) +
  ggforce::geom_arc_bar(data = ex[ex$bucket_id == 15,], # Whatever bucket you want
                        aes(start = 0, end = 2 * pi, fill2 = as.factor(bucket_id))) %>% 
  rename_geom_aes(new_aes = c("fill" = "fill2")) +
  scale_fill_manual(aesthetics = "fill2", values = "red", guide = "legend") +
  theme_void() +
  labs(fill = 'colour', fill2 = "highlight")
© www.soinside.com 2019 - 2024. All rights reserved.