R 中的条形图:奇怪的空第一列

Barplots in R: Strange Empty 1st Column

以下代码在 R 中创建了一个条形图。但是,第一列是空的。我不明白为什么......我的数据集中没有 NA 值。如何删除 "Bayview Column" 和 Y 轴之间的 space?

# 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
        )

这是带有第一个空列的结果条形图:

你有一个空白因子水平浮动,例如:

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