以下代码在R中创建Barplot。但是,第一列为空。我不明白为什么...我的数据集中没有NA值。如何删除“Bayview列”和Y轴之间的空间?
# 2. Bar Plot for Police District
barplot(xtabs(~sample$PoliceDistrict),
main="Police District Distribution of Incidents",
xlab="Number of Incidents in Police District",
ylab="Frequency",
col=rainbow(nlevels(as.factor(sample$PoliceDistrict))),
las=2,
# cex.lab=0.50 This is for the x-axis Label,
cex.names = 0.45
)
这是带有第一个空列的生成的Barplot:
最佳答案
您有一个空白的因子水平浮动,例如:
x <- factor(c("One","One","Two","Two","Two"), levels=c("","One","Two") )
levels(x)
#[1] "" "One" "Two"
barplot(table(x))
## EXTRA BAR PLOTTED
x <- droplevels(x)
# ?droplevels
# The function ‘droplevels’ is used to drop unused levels from a
# factor or, more commonly, from factors in a data frame.
levels(x)
#[1] "One" "Two"
barplot(table(x))
## FIXED
关于r - R中的Barplots:奇怪的空第一栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32981564/