我正在try 在特定的位置绘制一个有12种特定 colored颜色 的渐变色轮.其目的是为一项认知科学实验重现一个带有 colored颜色 而不是数字的钟面环境.所需的输出应如下所示:
我能够创建离散色轮如下:
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 = '')
)
然后,我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 = '')
)
如有任何帮助,我们不胜感激!谢谢!