I'm trying to draw a multi layered pie chart using the following code
df <- data.frame(a = c(4, 3, 3, 8, 1, 1, 10),
b = c("x", "x", "x", "y", "y", "y", "z"),
c = c("x1", "x2", "x3", "y1", "y2", "y3", "z1"))
ggplot(df, aes(x = b, y = a, fill = c))+
geom_bar(stat = "identity",
color = "black",
fill = c("yellow","blue","black","green","red","white","black"))+
coord_polar(theta="y")
The thing is, when I add the fill variable into the geom_bar I lose the legend, and can't add it at all, tried adding scale_fill_manual()
as some suggested but it still doesn't work for me, any idea why this is happening?
PS: is it possible to remove the spaces between each bar/circle?
You don't need the second fill
argument, the one in geom_bar
.
library(ggplot2)
df <- data.frame(a = c(4, 3, 3, 8, 1, 1, 10),
b = c("x", "x", "x", "y", "y", "y", "z"),
c = c("x1", "x2", "x3", "y1", "y2", "y3", "z1")
)
ggplot(df, aes(x = b, y = a, fill = c))+
geom_bar(stat = "identity",
width=1,#set width to remove white space, b/c the default is 0.9
color = "black",)+
coord_polar(theta="y")+ #Note you don't need the scale_fill_manual below; if omitted, r uses the default palette
scale_fill_manual(values = c("yellow","blue","black","green","red","white","black"))
)
Result:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.