Barplots in R: Strange Empty 1st Column

元气小坏坏 提交于 2020-01-04 02:34:40

问题


The following code creates a Barplot in R. However, the 1st column is empty. I don't understand why... There is no NA values in my data set. How can I remove the space between the "Bayview Column" and the Y-Axis?

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

Here is the resulting Barplot with the 1st empty column:


回答1:


You have a blank factor level floating about, e.g.:

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


来源:https://stackoverflow.com/questions/32981564/barplots-in-r-strange-empty-1st-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!