如何在R中的条形图中显示每个因素顶部的频率 [英] How to display the frequency at the top of each factor in a barplot in R

查看:20
本文介绍了如何在R中的条形图中显示每个因素顶部的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
将文本添加到 R 中的水平条形图,y-不同比例的轴?
注释条形上方的值(ggplot faceted)

使用下面的代码,我希望在每一列上方显示一个与该列的 y 值相对应的数字.换句话说,我试图让QnWeight_initial"在灰色条的顶部显示 593 等等......

Using the code below, I'm hoping to display a number above each column that corresponds to the y-value for that column. In other words, I'm trying to get "QnWeight_initial" to display 593 at the top of the gray bar and so and...

我的数据:

data<-structure(list(V1 = structure(c(2L, 1L), .Label = c("593", "QnWeight_initial"
), class = "factor"), V2 = structure(c(2L, 1L), .Label = c("566", 
"Head"), class = "factor"), V3 = structure(c(2L, 1L), .Label = c("535", 
"V1"), class = "factor"), V4 = structure(c(2L, 1L), .Label = c("535", 
"V2"), class = "factor"), V5 = structure(c(2L, 1L), .Label = c("535", 
"V3"), class = "factor"), V6 = structure(c(2L, 1L), .Label = c("482", 
"Left_Leg"), class = "factor"), V7 = structure(c(2L, 1L), .Label = c("474", 
"Left_Antenna"), class = "factor"), V8 = structure(c(2L, 1L), .Label = c("237", 
"Qn_Weight_Loss"), class = "factor"), V9 = structure(c(2L, 1L
), .Label = c("230", "Days_wrkr_eclosion"), class = "factor"), 
    V10 = structure(c(2L, 1L), .Label = c("81", "Growth_all"), class = "factor"), 
    V11 = structure(c(2L, 1L), .Label = c("79", "Growth_1_2"), class = "factor"), 
    V12 = structure(c(2L, 1L), .Label = c("62", "Growth_1_3"), class = "factor"), 
    V13 = structure(c(2L, 1L), .Label = c("60", "Growth_2_3"), class = "factor"), 
    V14 = structure(c(2L, 1L), .Label = c("51", "Right_Antenna"
    ), class = "factor"), V15 = structure(c(2L, 1L), .Label = c("49", 
    "Left_Leg_Remeasure"), class = "factor"), V16 = structure(c(2L, 
    1L), .Label = c("49", "Right_Leg"), class = "factor"), V17 = structure(c(2L, 
    1L), .Label = c("47", "Head_Remeasure"), class = "factor"), 
    V18 = structure(c(2L, 1L), .Label = c("46", "Left_Antenna_Remeasure"
    ), class = "factor")), .Names = c("V1", "V2", "V3", "V4", 
"V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13", "V14", 
"V15", "V16", "V17", "V18"), class = "data.frame", row.names = c(NA, 
-2L))
dat<-data.frame(fac=unlist(data[1,, drop=FALSE]), freqs=unlist(data[2,, drop=FALSE]))

剧情:

barplot( as.numeric( as.character(dat$freqs)) , main="Sample Sizes of Various Fitness Traits", xaxt='n', xlab='', width=0.85, ylab="Frequency")
par(mar=c(5,8,4,2))
labels<-unlist(data[1,,drop=FALSE])
text(1:18, par("usr")[3] -0.25, srt=90, adj=1,labels=labels,xpd=TRUE, cex=0.6)

推荐答案

您遇到了问题,因为 dat$freqs 是一个因素,即使它的打印表示看起来像"它是数字.(输入 str(foo) 几乎总是有帮助的——这里是 str(dat)str(dat$freqs) -- 以获得查看您正在使用的数据的真实结构.)

You are having problems because dat$freqs is a factor, even though it's printed representation 'looks like' it's numeric. (It's almost always helpful to type str(foo) -- here str(dat) or str(dat$freqs) -- to have a look at the real structure of the data you're working with.)

无论如何,一旦您将 dat$freq 转换为 "numeric" 类,构建图就变得简单了:

In any case, once you've converted dat$freq to class "numeric", constructing the plot becomes straightforward:

## Make the frequencies numbers (rather than factors)
dat$freqs <- as.numeric(as.character(dat$freqs))
## Find a range of y's that'll leave sufficient space above the tallest bar
ylim <- c(0, 1.1*max(dat$freqs))
## Plot, and store x-coordinates of bars in xx
xx <- barplot(dat$freqs, xaxt = 'n', xlab = '', width = 0.85, ylim = ylim,
              main = "Sample Sizes of Various Fitness Traits", 
              ylab = "Frequency")
## Add text at top of bars
text(x = xx, y = dat$freqs, label = dat$freqs, pos = 3, cex = 0.8, col = "red")
## Add x-axis labels 
axis(1, at=xx, labels=dat$fac, tick=FALSE, las=2, line=-0.5, cex.axis=0.5)

这篇关于如何在R中的条形图中显示每个因素顶部的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆