我如何在 R 中绘制包含字符和数字的数据框?
How can i barplot a dataframe contains character an numeric in R?
我有这个数据框
product Total
AF064 21
AF065 24
AF066 1
AF067 13
AF068 6
AF069 3
AF070 5
AF071 1
AF072 3
AF073 3
AF074 5
AF075 2
AF076 28
AF077 0
AF078 3
AF079 10
AF080 0
AF081 13
AF082 0
AF083 3
AF084 3
AF085 2
AF086 3
AF087 0
AF088 1
AF089 1
AF090 2
AF091 4
AF092 2
AF093 3
AF094 2
AF095 3
AF096 1
AF097 2
AF098 2
AF099 1
AF100 21
AF101 1
AF102 3
我想根据这个数据框制作条形图。
我的密码是
barplot(product,Total)
**Error in -0.01 * height : non-numeric argument to binary operator**
我也试过了
barplot(dataframe)
**Error in barplot.default(dataframe) :
'height' must be a vector or a matrix**
我也尝试了 as.character
的产品,但仍然无法绘制图表。
非常感谢你们的帮助,非常感谢。
您希望条形图的参数是高度向量。你可以使用这样的东西:
barplot(dataframe[,2], names.arg=dataframe[,1])
你的第一次尝试几乎是正确的,
attach(dataframe)
barplot(Total,names.arg=product)
会做的。
另一种可能性是将 df 转换为矩阵并将其提供给 barplot()
函数。
barplot()
函数会为矩阵的每一列绘制一个柱状图,所以需要transpose立即转换dataframe
m <- t( matrix(dataframe[,2],dimnames=dataframe[1]) )
barplot(m)
如果您的数据框中有多个列(例如 2 到 3),您可以将它们全部放入矩阵中,作为行:
m <- t(as.matrix(dataframe[,2:3]))
colnames(m) <- dataframe[,1]
barplot(m,legend=T)
至于你最后的评论,你可以把标签横过来,稍微调整一下大小。 IE。直接使用数据框
attach(dataframe)
barplot(Total,names.arg=product,cex.names=0.6,las=2)
或使用矩阵m
barplot(m,cex.names=0.6,las=2)
我有这个数据框
product Total
AF064 21
AF065 24
AF066 1
AF067 13
AF068 6
AF069 3
AF070 5
AF071 1
AF072 3
AF073 3
AF074 5
AF075 2
AF076 28
AF077 0
AF078 3
AF079 10
AF080 0
AF081 13
AF082 0
AF083 3
AF084 3
AF085 2
AF086 3
AF087 0
AF088 1
AF089 1
AF090 2
AF091 4
AF092 2
AF093 3
AF094 2
AF095 3
AF096 1
AF097 2
AF098 2
AF099 1
AF100 21
AF101 1
AF102 3
我想根据这个数据框制作条形图。
我的密码是
barplot(product,Total)
**Error in -0.01 * height : non-numeric argument to binary operator**
我也试过了
barplot(dataframe)
**Error in barplot.default(dataframe) :
'height' must be a vector or a matrix**
我也尝试了 as.character
的产品,但仍然无法绘制图表。
非常感谢你们的帮助,非常感谢。
您希望条形图的参数是高度向量。你可以使用这样的东西:
barplot(dataframe[,2], names.arg=dataframe[,1])
你的第一次尝试几乎是正确的,
attach(dataframe)
barplot(Total,names.arg=product)
会做的。
另一种可能性是将 df 转换为矩阵并将其提供给 barplot()
函数。
barplot()
函数会为矩阵的每一列绘制一个柱状图,所以需要transpose立即转换dataframe
m <- t( matrix(dataframe[,2],dimnames=dataframe[1]) )
barplot(m)
如果您的数据框中有多个列(例如 2 到 3),您可以将它们全部放入矩阵中,作为行:
m <- t(as.matrix(dataframe[,2:3]))
colnames(m) <- dataframe[,1]
barplot(m,legend=T)
至于你最后的评论,你可以把标签横过来,稍微调整一下大小。 IE。直接使用数据框
attach(dataframe)
barplot(Total,names.arg=product,cex.names=0.6,las=2)
或使用矩阵m
barplot(m,cex.names=0.6,las=2)