我想给饼图添加颜色。我用了scale_fill_manual(values = c("orange", "red","blue", "yellow", "green"))
。我想为苹果提供红色,橙色为橙色和蓝色为葡萄等。但我的饼图看起来不同。
d1 <- data.frame(
labels = c("orange","apple","grape","pineapple","muskmelon"),
value = c(1632,29,1491,1991,29)
)
d1$pos <- cumsum(d1$value) - 0.5*d1$value
p1 <-ggplot(d1, aes(x="", y = value, fill = labels))
p1 <- p1 + geom_bar(stat="identity", width=1)
p1 <- p1 + ggtitle("fruits")
p1 <- p1 + panel_border()
p1 <- p1 + coord_polar(theta = "y")
p1 <- p1 + xlab("")
p1 <- p1 + ylab("")
p1 <- p1 + geom_text(aes(x="", y=pos,label=labels), size=10)
p1 <- p1 + scale_fill_manual(values = c("orange", "red","blue", "yellow", "green"))
p1 <- p1 + theme(legend.position="none",
legend.title=element_blank(),
axis.line=element_blank(),axis.text.x=element_blank(),
axis.ticks=element_blank(),
plot.title = element_text(lineheight=3, face="bold", color="black", size=25))
答案 0 :(得分:1)
从help(scale_fill_manual)
,您可以将命名向量传递给values
参数:
p1 + scale_fill_manual(values = c("orange" = "orange",
"apple" = "red",
"grape" = "blue",
"pineapple" = "yellow",
"muskmelon" = "green"))
答案 1 :(得分:1)
这可能有点太晚了。我知道这里讨论的软件包是ggplot2
,但只是分享我如何使用内置的饼图功能。首先,我将指定颜色名称以对应数据框中的每个标签:
d1$colours <- c("orange", "red", "blue", "yellow", "green")
这样我的d1
看起来像这样:
labels value pos colours
orange 1632 816.0 orange
apple 29 1646.5 red
grape 1491 2406.5 blue
pineapple 1991 4147.5 yellow
muskmelon 29 5157.5 green
然后,使用pie
函数绘图:
pie(x = d1$value, labels = d1$labels, col = d1$colours)