我正在try 在特定的位置绘制一个有12种特定 colored颜色 的渐变色轮.其目的是为一项认知科学实验重现一个带有 colored颜色 而不是数字的钟面环境.所需的输出应如下所示:

Desired output

我能够创建离散色轮如下:

library(ggplot2)

data <- data.frame(
  number=c(1:12),
  count=rep(10, 12),
  cs=c('#FE2712', '#FC600A', '#FB9902', '#FCCC1A', '#FEFE33', '#B2D732', 
       '#66B032', '#347C98', '#0247FE', '#4424D6', '#8601AF', '#C21460'))

data$fraction = data$count / sum(data$count)

# Compute cumulative percentages (top of each rectangle)
data$ymax = cumsum(data$fraction)

# Compute bottom of each rectangle
data$ymin = c(0, head(data$ymax, n=-1))

# 15 degrees in radians
deg15 <- 0.26179999

(basicwheel <-
  ggplot(data, aes(ymax=ymax, ymin=ymin, xmax=1, xmin=0, fill= as.factor(number), color='black')) +
  geom_rect(inherit.aes = T) +
  coord_polar(theta="y", start=deg15) +
  xlim(c(0, 1))+
  scale_color_manual(values = 'black')+
  scale_fill_manual(values= cs)+
  theme_void()+
  theme(legend.position = '')
)

Here is the discrete color wheel

然后,我try 通过在每个切片的中心保持纯 colored颜色 值,同时向一侧的前一切片的纯 colored颜色 和另一侧的下一切片的 colored颜色 添加发散渐变褪色来调整该情节.

我用与自己的、下一个和前一个切片的 colored颜色 相关联的 colored颜色 值创建了一个矩阵.

colmat <- data.frame(number = numeric(length(unique(data$number))),
                     ymax = numeric(length(unique(data$number))),
                     ymin = numeric(length(unique(data$number))),
                     previous_color = numeric(length(unique(data$number))),
                     next_color = numeric(length(unique(data$number))),
                     own_color = numeric(length(unique(data$number))))

for (i in 1:length(unique(data$number))) {
  d <- data %>% subset(as.numeric(number) == i)
  iminusone <- as.numeric(i) - 1
  iplusone <- as.numeric(i) + 1
  previousnr <- ifelse(iminusone > 0, data[which(as.numeric(data$number) == iminusone),]$number, 12)
  nxtnr <- ifelse(iplusone < 13, data[which(as.numeric(data$number) == iplusone),]$number, 1)
  colmat[i, 'number'] <- i
  
  previous_color <- colmat[i, 'previous_color'] <- data[which(as.numeric(data$number)  == previousnr),]$cs
  next_color <- colmat[i, 'next_color'] <- data[which(as.numeric(data$number) == nxtnr),]$cs
  owncolor <- colmat[i, 'own_color'] <- data[which(as.numeric(data$number) == i),]$cs
  ymax <- colmat[i, 'ymax'] <- data[which(as.numeric(data$number) == i),]$ymax
  ymin <- colmat[i, 'ymin'] <- data[which(as.numeric(data$number) == i),]$ymin
  
}

head(colmat)

然而,我肯定还是遗漏了一些东西,因为下面的图没有在切片中显示所需的渐变.

(gwheel <-
    ggplot(colmat, aes(ymax=ymax, ymin=ymin, xmax=1, xmin=0, fill=number)) +
    geom_rect() +
    coord_polar(theta="y", start=deg15) +
    xlim(c(0, 1))+
    scale_fill_gradient2(high = colmat$previous_color, mid = colmat$own_color, low = colmat$next_color)+
    theme_void()+
    theme(legend.position = '')
)

The result of this latter attempt.

如有任何帮助,我们不胜感激!谢谢!

推荐答案

我认为这为您提供了所需图像的合理复制品:

library(ggplot2)

cs <- c('#FE2712', '#FC600A', '#FB9902', '#FCCC1A', '#FEFE33', 
        '#B2D732', '#66B032', '#347C98', '#0247FE', '#4424D6', 
        '#8601AF', '#C21460', '#FE2712')

vals <- seq(0, 100, 0.1)

ggplot(mapping = aes(x = 1, fill = vals, group = vals)) +
  geom_bar(aes(color = after_scale(fill)), linewidth = 0.2) +
  scale_fill_gradientn(colors = cs, guide = 'none') +
  coord_polar(theta = 'y', start = pi/2) +
  theme_void()

enter image description here

R相关问答推荐

在R中使用自定义函数时如何删除该函数的一部分?

从R中的另一个包扩展S3类的正确方法是什么

变量计算按R中的行更改

如何修复R码的置换部分?

pickerInput用于显示一条或多条geom_hline,这些线在图中具有不同 colored颜色

bslib::card_header中的shine::downloadButton,图标而不是文本

S用事件解决物质平衡问题

为什么舍入POSIXct会更改能力以匹配等效的POSIXct?

用关联字符串替换列名的元素

2个Rscript.exe可执行文件有什么区别?

仅在Facet_WRAP()中的相应方面包含geom_abline()

如何将Which()函数用于管道%>;%

如何计算R glm probit中的线性预测因子?

在gggraph中显示来自不同数据帧的单个值

我将工作代码重构为一个函数--现在我想不出如何传递轴列参数

错误包arrowR:READ_PARQUET/OPEN_DATASET&QOT;无法反序列化SARIFT:TProtocolException:超出大小限制&Quot;

有毒元素与表观遗传年龄的回归模型

计算多变量的加权和

R-使用stri_trans_General()将其音译为德语字母

R中的交叉表