【发布时间】:2016-04-25 10:37:40
【问题描述】:
我没有使用r,但我最近决定用它来绘制图表——因为它的强大功能。
我想让我的图表更好。具体来说,我会在条形图上绘制数字。
我看到Adding labels to ggplot bar chart 并尝试使用
geom_text(aes(x=years, y=freq, ymax=freq, label=value,
hjust=ifelse(sign(value)>0, 1, 0)),
position = position_dodge(width=1)) +
但是数字没有显示出来。
这是我的代码:
# Load ggplot2 graphics package
library(ggplot2)
# Create dataset
dat <- data.frame(years = c("1991", "1993", "1997", "2001", "2005", "2007", "2011", "2015"),
freq = c(43.20, 52.13, 47.93, 46.29, 40.57, 53.88, 48.92, 50.92))
# Plot dataset with ggplot2
ggplot(dat, aes(years, freq)) + geom_bar(stat = "identity", width=0.55)
+ labs(x="Year",y="") + theme_classic()
# Comma as decimal mark
format(df, decimal.mark=",")
【问题讨论】: