[英]Legend disappears when I fill the bars on 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",
color = "black",
fill = c("yellow","blue","black","green","red","white","black"))+
coord_polar(theta="y")
问题是,当我将填充变量添加到geom_bar中时,我失去了图例,甚至根本无法添加图例,尝试按照某些建议添加scale_fill_manual()
,但它仍然对我不起作用,这是为什么发生了什么?
PS:是否可以删除每个条/圆之间的空间?
您不需要第二个fill
参数,即geom_bar
中的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"))
)
结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.