繁体   English   中英

当我填充ggplot2上的条形时,图例消失

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM