以下代码在 R 中创建了一个条形图。但是,第一列是空的。我不明白为什么......我的数据集中没有 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
)
这是带有第一个空列的结果条形图:
最佳答案
你有一个空白的因子水平 float ,例如:
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 : Strange Empty 1st Column 中的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32981564/