如何使用其中一列已包含频率/计数的数据制作条形图 [英] How to make a barplot with data where one of the columns already contains the frequencies/counts

查看:42
本文介绍了如何使用其中一列已包含频率/计数的数据制作条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制具有多个特征中每一个的数据的样本数量.我实际上已经有一列包含这些样本数量的数据,并且只是希望将其绘制为频率".事实上,我不太确定频率数据来自哪里(见下面的代码).请让我知道我是否可以澄清任何事情.非常感谢!:

I'm trying to plot the number of samples that have data for for each of several traits. I actually already have a column of data containing these numbers of samples, and was simply hoping to plot it as the "frequency". As it is, I'm not quite sure where the frequency data is coming from (see code below). Please let me know whether I can clarify anything. Many thanks!:

##my data
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]))
t<-table(rep(as.character(dat[, 1]), dat[, 2]))

##the plot I'm making at the moment
barplot(t, main="Sample Sizes of Various Fitness Traits", xaxt='n', xlab='', width=0.85)
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)


##The kind of plot I'm looking to make
par(las=2) # make label text perpendicular to axis
par(mar=c(5,8,4,2)) # increase y-axis margin.

print(mtcars$gear)
counts <- table(mtcars$gear)
print(counts)
barplot(counts, main="Car Distribution", names.arg=c("3 Gears", "4 Gears", "5   Gears"), cex.names=0.8)

推荐答案

这构成了一个明显合理的情节.您可能会问如何使用频率"标记 y 轴.

That makes an apparently sensible plot. It appears you might be asking how to label a y-axis with "Frequency".

barplot( t, main="Sample Sizes of Various Fitness Traits", 
            xaxt='n', xlab='', width=0.85, ylab="Frequency")
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)

Or::: 你问代码的作用是因为你从别人那里复制了它,但你并不真正理解它?创建对象t"的 table 函数计算唯一类别中的项目数.短语 rep(as.character(dat[, 1]), dat[, 2])) 有点晦涩,但它重复 V2 的每个值的次数与dat 中 V2 因子表示的数字编码 .... 换句话说,很可能是无稽之谈.

Or::: Were you asking what the code does because you copied it from someone else and you do not really understand it? The table function that creates the object "t" counts up the number of items in the unique categories. The phrase rep(as.character(dat[, 1]), dat[, 2])) is a bit obscure but it is repeating each value of V2 the same number of times as is in the numeric coding for the factor representation of V2 in dat .... in other words something more than likely to be nonsense.

或者您是在问它是否以合理的方式表示数据?(它没有.)有一个 R-FAQ 是关于如何在不经意间将分解的变量转换回数字的:

Or are you asking if it represents the data in a sensible way? (It does not.) There is an R-FAQ about how to convert factorized variables back to numeric when they have been inadvertently made as such:

 barplot( as.numeric( as.character(dat$freqs)) , 
    main="Sample Sizes of Various Fitness Traits", 
    xaxt='n', xlab='', width=0.85, ylab="Frequency")
 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)

这篇关于如何使用其中一列已包含频率/计数的数据制作条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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