【问题标题】:Add a information in ggplot legend from data.frame从 data.frame 在 ggplot 图例中添加信息
【发布时间】:2016-11-07 05:34:58
【问题描述】:

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

这是我的代码:

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"))

这是我的结果:

我想在传说中这样写:

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

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

【问题讨论】:

  • 你可以使用 labels 参数到 scale_fill_brewer
  • @RichardTelford 是的,但问题是当我输入scale_fill_brewer(labels = z$a) 时,我得到了文字,但不是正确的价值文字。我不知道如何在没有手写的情况下将正确的传感器用于价值。

标签: r dataframe ggplot2


【解决方案1】:

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

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"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 2018-11-26
    • 2020-06-10
    相关资源
    最近更新 更多