r语言 条形图上有数值

Python017

r语言 条形图上有数值,第1张

用barplot() 做条形图

然后用 text( )标数值即可

举例:

x=round(runif(10)*10,1)  #取10个随机数值

barplot(x)                 #绘图

text(1:10*1.2-.5,x,x)   #加数值标识

ggplot2包可以用来绘图,其中的geom_text函数可以设置标签:library(plyr)library(ggplot2)library(scales)dtf <- data.frame(x = c("ETB", "PMA", "PER", "KON", "TRA", "DDR", "BUM", "MAT", "HED", "EXP"), y = c(.02, .11, -.01, -.03, -.03, .02, .1, -.01, -.02, 0.06))ggplot(dtf, aes(x, y)) + geom_bar(stat = "identity", aes(fill = x)) + geom_text(aes(label = paste(y * 100, "%"), vjust = ifelse(y >= 0, 0, 1))) + scale_y_continuous("Anteil in Prozent", labels = percent_format())