我正在尝试绘制具有针对几个特征中的每个特征的数据的样本数量。我实际上已经有一列包含这些样本数的数据,并且只是希望将其绘制为“频率”。实际上,我不太确定频率数据的来源(见下面的代码)。请告诉我是否可以澄清任何内容。非常感谢!:
##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)
答案 0 :(得分:2)
这显然是一个明智的情节。看来你可能会问如何用“频率”标记y轴。
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)
或者:::您是否在询问代码的作用,因为您从其他人那里复制了它并且您并不真正理解它?创建对象“t”的table
函数会计算唯一类别中的项目数。短语rep(as.character(dat[, 1]), dat[, 2]))
有点模糊,但它重复V2的每个值与dat
中V2的因子表示的数字编码中的重复次数相同....换句话说很可能是胡说八道。
或者您是否在询问它是否以合理的方式表示数据? (它没有。)有一个R-FAQ,关于如何在无意中将因子化变量转换为数字时将其转换为数字:
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)
答案 1 :(得分:1)
尝试从数据对象中提取一些有用的数据。 (问题是您的数据列是所有因素,标题行应该是数据的第一行。这应该可以使用read.table(...,header=T)
# converting from factor to character or numeric as required
measure <- unlist(lapply(data[1,], as.character))
value <- unlist(lapply(data[2,], function(i){as.numeric(as.character(i))}))
# set names appropriately
names(value) <- measure
# the plot
par(las=2) # make label text perpendicular to axis
par(mar=c(5,8,4,2)) # increase y-axis margin.
barplot(value)