I want to give colors to the piechart. I used scale_fill_manual(values = c("orange", "red","blue", "yellow", "green"))
. I want to give red color for apple, orange color for orange and blue color for grape etc. But my pie chart looks different.
我想给饼图给出颜色。我使用了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))
2 个解决方案
#1
1
From help(scale_fill_manual)
, you can pass a named vector to the values
parameter:
从help(scale_fill_manual),您可以将命名向量传递给values参数:
p1 + scale_fill_manual(values = c("orange" = "orange",
"apple" = "red",
"grape" = "blue",
"pineapple" = "yellow",
"muskmelon" = "green"))
#2
1
This may be a little too late. I understand that the package in question here is ggplot2
but just to share how I would do it using the built-in pie function. First, I would assign the colour names to correspond each label in the data frame:
这可能有点太晚了。我知道这里讨论的包是ggplot2,但只是分享我如何使用内置的派对功能。首先,我将指定颜色名称以对应数据框中的每个标签:
d1$colours <- c("orange", "red", "blue", "yellow", "green")
such that my d1
looks like this:
这样我的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
Then, plot using the pie
function:
然后,使用饼图函数绘图:
pie(x = d1$value, labels = d1$labels, col = d1$colours)
#1
1
From help(scale_fill_manual)
, you can pass a named vector to the values
parameter:
从help(scale_fill_manual),您可以将命名向量传递给values参数:
p1 + scale_fill_manual(values = c("orange" = "orange",
"apple" = "red",
"grape" = "blue",
"pineapple" = "yellow",
"muskmelon" = "green"))
#2
1
This may be a little too late. I understand that the package in question here is ggplot2
but just to share how I would do it using the built-in pie function. First, I would assign the colour names to correspond each label in the data frame:
这可能有点太晚了。我知道这里讨论的包是ggplot2,但只是分享我如何使用内置的派对功能。首先,我将指定颜色名称以对应数据框中的每个标签:
d1$colours <- c("orange", "red", "blue", "yellow", "green")
such that my d1
looks like this:
这样我的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
Then, plot using the pie
function:
然后,使用饼图函数绘图:
pie(x = d1$value, labels = d1$labels, col = d1$colours)