我正在尝试创建一个圆环图,我很难添加颜色渐变。我希望能够采用所有不同的类别,并将它们从任何颜色淡化为白色。有一个简单的方法吗?
data<-data.frame(count=c(39,36,19,6), category=c("a","b","c","d"))
data$fraction = data$count / sum(data$count)
data = data[order(data$fraction), ]
data$ymax = cumsum(data$fraction)
data$ymin = c(0, head(data$ymax, n=-1))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Create Plot
fill <- c("blue3","cyan3","darkgrey","forestgreen")
p1 = ggplot(data, aes(fill=category, ymax=ymax, ymin=ymin, xmax=4, xmin=3.5))
+ geom_rect(colour="White") +
coord_polar(theta="y") +
scale_fill_manual(values=fill)+
theme_bw()+
theme(panel.grid=element_blank())+
theme(axis.ticks=element_blank()) +
xlim(c(0, 4)) +
theme(axis.text=element_blank()) +
labs(title="donut plot")
print(p1)
答案 0 :(得分:0)
Use the alpha
values to control the shading within each category
.
library(ggplot2)
library(dplyr)
N <- 100
fill <- c("blue3","cyan3","darkgrey","forestgreen")
data <- data.frame(count = c(39,36,19,6), category = c("a","b","c","d"))
data$fraction <- data$count / sum(data$count)
data <- data[order(data$fraction), ]
data$ymax <- cumsum(data$fraction)
data$ymin <- c(0, head(data$ymax, n = -1))
data2 <-
data.frame(count = rep(data$count, each = N),
category = rep(data$category, each = N),
ymin = c(0,
seq(0.00, 0.06, length = N),
seq(0.06, 0.25, length = N),
seq(0.25, 0.61, length = N),
seq(0.61, 1.00, length = N)[-N]),
ymax = c(seq(0.00, 0.06, length = N)[-1],
seq(0.06, 0.25, length = N),
seq(0.25, 0.61, length = N),
seq(0.61, 1.00, length = N),
1.00))
data2$y <- with(data2, (ymin + ymax)/2)
data2 <-
data2 %>%
group_by(category) %>%
mutate(alpha = (y - min(y)) / max(y)) %>%
ungroup()
ggplot(data2) +
aes(ymax = ymax, ymin = ymin, xmax = 4, xmin = 3.5, fill = category, alpha = alpha) +
geom_rect() +
coord_polar(theta = "y") +
theme_bw()+
theme(panel.grid=element_blank()) +
theme(axis.ticks=element_blank()) +
xlim(c(0, 4)) +
theme(axis.text=element_blank()) +
labs(title="donut plot") +
scale_fill_manual(values = fill) +
guides(alpha = "none")
答案 1 :(得分:0)
It is not possible to do this directly, as far as I know, but you could achieve a similar effect by expanding your data frame and setting an alpha value dependent on the radius. Something like this...
data<-data.frame(count=c(39,36,19,6), category=c("a","b","c","d"))
data$fraction = data$count / sum(data$count)
data = data[order(data$fraction), ]
data$ymax = cumsum(data$fraction)
data$ymin = c(0, head(data$ymax, n=-1))
data <- as.data.frame(lapply(data,rep,10))
data$xmin <- rep(0:9,each=4)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Create Plot
fill <- c("blue3","cyan3","darkgrey","forestgreen")
p1 = ggplot(data, aes(fill=category, ymax=ymax, ymin=ymin, xmin=xmin, xmax=xmin+1)) + geom_rect(aes(alpha=xmin/9)) +
coord_polar(theta="y") +
scale_fill_manual(values=fill)+
scale_alpha_continuous(guide=FALSE)+
theme_bw()+
theme(panel.grid=element_blank())+
theme(axis.ticks=element_blank()) +
theme(axis.text=element_blank()) +
labs(title="donut plot")
print(p1)