我之前问过一个问题,但现在我想知道如何将标签放在栏上方。
dataframe <- c (1,1.2,40,1000,36.66,400.55,100,99,2,1500,333.45,25,125.66,141,5,87,123.2,61,93,85,40,205,208.9)
Run Code Online (Sandbox Code Playgroud)
更新日期
更新
按照同事的指导,我正在更新问题。
我有一个数据库,我想计算该基数的给定值在预定义范围内出现的频率,例如:0-50、50-150、150-500、500-2000。
在帖子中(如何创建具有预定义的非均匀间隔的频率直方图?)我设法做到了这一点,但我不知道如何在条形上方添加标签。我试过:
barplort (data, labels = labels),但没有成功。
我使用 barplot 因为该帖子推荐了我,但如果可以使用 ggplot 来完成,那也很好。
根据第一个问题的答案,这是一种向 Base R 图添加text()元素的方法,该元素用作每个条形图的标签(假设您想要将 x 上已有的信息加倍)轴)。
data <- c(1,1.2,40,1000,36.66,400.55,100,99,2,1500,333.45,25,125.66,141,5,87,123.2,61,93,85,40,205,208.9)
# Cut your data into categories using your breaks
data <- cut(data,
breaks = c(0, 50, 150, 500, 2000),
labels = c('0-50', '50-150', '150-500', '500-2000'))
# Make a data table (i.e. a frequency count)
data <- table(data)
# Plot with `barplot`, making enough space for the labels
p <- barplot(data, ylim = c(0, max(data) + 1))
# Add the labels with some offset to be above the bar
text(x = p, y = data + 0.5, labels = names(data))
Run Code Online (Sandbox Code Playgroud)

如果您要的是 y 值,则可以更改传递给labels参数的内容:
p <- barplot(data, ylim = c(0, max(data) + 1))
text(x = p, y = data + 0.5, labels = data)
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2020 年 12 月 11 日创建