按照以下R cookbook
中的示例进行操作dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour="black", stat="identity") +
guides(fill=FALSE)
如何将total_bill的值(14.89,17.23)添加到每个条形图的顶部,并将值四舍五入为1十进制,如-14.9,17.2
答案 0 :(得分:3)
你可以这样做:
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour="black", stat="identity") +
geom_text(aes(label = sprintf("%.1f", total_bill), y= total_bill), vjust = 3)+
guides(fill=FALSE)
您可以调整vjust
向上或向下移动标签。