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

时间:2015-10-06 23:48:19

标签: r formatting bar-chart

以下代码在R中创建Barplot。但是,第1列为空。我不明白为什么......我的数据集中没有NA值。如何删除" Bayview Column"之间的空间。和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:

enter image description here

1 个答案:

答案 0 :(得分:1)

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

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