在data.frame

时间:2016-07-05 11:35:46

标签: r dataframe ggplot2

我想在图例中添加哪个传感器具有此值的信息。

这是我的代码:

z <- data.frame(
       a=c("sensor 1","sensor 2","sensor 3","sensor 4","sensor 5","sensor 6","sensor 7","sensor 8"), 
       b=c(50, 60, 70, 20,90,110,30,100)
     )

cxc <- ggplot(z, aes(x=a, y=b, fill=factor(b))) + 
         geom_bar(width = 1,stat="identity",colour = "black")  + 
         scale_fill_brewer(palette="Blues",type = "seq")+
         geom_text(aes(y = b/2,label = b),color = "red",size = 5)
cxc + coord_polar() + 
  theme_linedraw() + 
  theme(axis.ticks =element_blank(), axis.text.y =element_blank(), axis.title=element_blank(), axis.text.x=element_text(size = 12,angle = 45)) +
  guides(fill = guide_legend(title = "Value and sensor"))

这是我的结果: enter image description here

我想在传说中这样写:

  • 20(传感器4)
  • 30(传感器7)
  • 50(传感器1)

我希望保留这种颜色和图形样式。

1 个答案:

答案 0 :(得分:3)

使用labels参数scale_fill_brewerorder标签,以便它们按照正确的顺序排列。

cxc <- ggplot(z, aes(x=a, y=b, fill=factor(b))) + 
  geom_bar(width = 1,stat="identity",colour = "black")  + 
  scale_fill_brewer(palette="Blues",type = "seq", labels = paste0(z$b, " (", z$a, ")")[order(z$b)])+
  geom_text(aes(y = b/2,label = b),color = "red",size = 5)
cxc + coord_polar() + 
  theme_linedraw() + 
  theme(axis.ticks =element_blank(), axis.text.y =element_blank(), axis.title=element_blank(), axis.text.x=element_text(size = 12,angle = 45)) +
  guides(fill = guide_legend(title = "Value and sensor"))