ggplot2 带颜色渐变的圆环图
ggplot2 donut chart with gradient of colors
我想构建一个标准函数来在 R 中绘制 圆环图。following question helps me a lot, but I can't colour the plot with a gradient of n colors ranging from color 1 and color 2。
我的代码:
donut_chart <- function(data,variable1,variable2,col1="red",col2="yellow")
{
# Add addition columns to data, needed for donut plot.
data[,"fraction"] = data[,"variable2"]/ sum(data[,"variable2"])
data[,"ymax"] = cumsum(data[,"fraction"])
data[,"ymin"] = c(0, head(data[,"ymax"], n = -1))
# Palette
colfunc <- colorRampPalette(c(col1,col2))
# Donut plot
ggplot(data, aes(fill = variable1, ymax = ymax, ymin = ymin, xmax = 4, xmin = 3)) +
geom_rect(colour = "white", show_guide = FALSE) +
coord_polar(theta = "y") + xlim(c(0, 4)) +
scale_fill_manual(values=c(colfunc(levels(data[,"variable1"])))+
theme_bw() +
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
geom_text(aes(x = 3.5, y = ((ymin+ymax)/2), label = type),colour="white",size=6,fontface="bold") +
xlab("") +
ylab("")
}
data.frame:
ad = data.frame(
type = c("Poster", "Billboard", "Bus", "Digital"),
n = c(529, 356, 59, 81)
)
输出代码:
donut_chart(ad,"type","n")
输出图仅用红色着色。
创建函数时要小心。
有时您将变量写为字符,有时则相反。还要注意缺少括号。
这似乎有效:
donut_chart <- function(data,variable1,variable2,col1="red",col2="yellow")
{
# Add addition columns to data, needed for donut plot.
data[,"fraction"] = data[,variable2]/ sum(data[,variable2])
data[,"ymax"] = cumsum(data[,"fraction"])
data[,"ymin"] = c(0, head(data[,"ymax"], n = -1))
# Palette
colfunc <- colorRampPalette(c(col1,col2))
# Donut plot
ggplot(data, aes_string(fill = variable1, ymax = "ymax", ymin = "ymin", xmax = 4, xmin = 3)) +
geom_rect(colour = "white", show.legend = FALSE) +
coord_polar(theta = "y") + xlim(c(0, 4)) +
scale_fill_manual(values=c(colfunc(length(data[,variable1]))))+
theme_bw() +
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
geom_text(aes(x = 3.5, y = ((ymin+ymax)/2), label = type),colour="black",size=6,fontface="bold") +
xlab("") +
ylab("")
}
donut_chart(ad,"type","n")
这给出了(我改变了文本的颜色,因为白色的白色很难阅读):
我想构建一个标准函数来在 R 中绘制 圆环图。following question helps me a lot, but I can't colour the plot with a gradient of n colors ranging from color 1 and color 2。
我的代码:
donut_chart <- function(data,variable1,variable2,col1="red",col2="yellow")
{
# Add addition columns to data, needed for donut plot.
data[,"fraction"] = data[,"variable2"]/ sum(data[,"variable2"])
data[,"ymax"] = cumsum(data[,"fraction"])
data[,"ymin"] = c(0, head(data[,"ymax"], n = -1))
# Palette
colfunc <- colorRampPalette(c(col1,col2))
# Donut plot
ggplot(data, aes(fill = variable1, ymax = ymax, ymin = ymin, xmax = 4, xmin = 3)) +
geom_rect(colour = "white", show_guide = FALSE) +
coord_polar(theta = "y") + xlim(c(0, 4)) +
scale_fill_manual(values=c(colfunc(levels(data[,"variable1"])))+
theme_bw() +
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
geom_text(aes(x = 3.5, y = ((ymin+ymax)/2), label = type),colour="white",size=6,fontface="bold") +
xlab("") +
ylab("")
}
data.frame:
ad = data.frame(
type = c("Poster", "Billboard", "Bus", "Digital"),
n = c(529, 356, 59, 81)
)
输出代码:
donut_chart(ad,"type","n")
输出图仅用红色着色。
创建函数时要小心。 有时您将变量写为字符,有时则相反。还要注意缺少括号。 这似乎有效:
donut_chart <- function(data,variable1,variable2,col1="red",col2="yellow")
{
# Add addition columns to data, needed for donut plot.
data[,"fraction"] = data[,variable2]/ sum(data[,variable2])
data[,"ymax"] = cumsum(data[,"fraction"])
data[,"ymin"] = c(0, head(data[,"ymax"], n = -1))
# Palette
colfunc <- colorRampPalette(c(col1,col2))
# Donut plot
ggplot(data, aes_string(fill = variable1, ymax = "ymax", ymin = "ymin", xmax = 4, xmin = 3)) +
geom_rect(colour = "white", show.legend = FALSE) +
coord_polar(theta = "y") + xlim(c(0, 4)) +
scale_fill_manual(values=c(colfunc(length(data[,variable1]))))+
theme_bw() +
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
geom_text(aes(x = 3.5, y = ((ymin+ymax)/2), label = type),colour="black",size=6,fontface="bold") +
xlab("") +
ylab("")
}
donut_chart(ad,"type","n")
这给出了(我改变了文本的颜色,因为白色的白色很难阅读):