我正在尝试在特定位置绘制具有 12 种特定颜色的渐变色轮。目的是为认知科学实验重现带有颜色而不是数字的表盘环境。所需的输出应该如下所示:
我能够创建离散色轮,如下所示:
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 = '')
)
然后,我尝试通过保持每个切片中心的纯色值来调整该图,同时添加发散渐变,一侧渐变为前一个切片的纯色,另一侧渐变为下一个切片的颜色侧面。
我创建了一个矩阵,其中包含与自己的、下一个和上一个切片的颜色相关联的颜色值。
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 = '')
)
如有任何帮助,我们将不胜感激!谢谢!
我认为这为您提供了所需图像的合理复制品:
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()