问题
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