2017-01-28 62 views
0

繼如何添加用於在與值每個條舍入到內部的頂部被顯示total_bill的值(14.89,17.23)的從R cookbook- [R GGPLOT2 - 增值柱狀圖

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) 

下面的例子1位小數像 - 14.9,17.2

Figure with above code

回答

2

你可以這樣做:

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以向上或向下移動標籤。