本文介绍了从data.frame在ggplot图例中添加信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在图例中添加哪个传感器具有此值的信息.
I want to add information in legend which sensor has this value.
这是我的代码:
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)
我要保留这种颜色和图形样式.
I want keep this colors and graph style.
推荐答案
使用 labels
自变量来 scale_fill_brewer
,并 order
这样标签它们以正确的顺序出现.
Use the labels
argument to scale_fill_brewer
, and order
the labels so they come in the correct order.
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"))
这篇关于从data.frame在ggplot图例中添加信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!