我想绘制一个条形图,其中轴x为月数,而轴y为变量值。该变量在某些月份可以为null。如何强制轴x始终显示12个月?
使用V数据框,如:
month variable
1 125
2 45
3 158
4 15
5 58
6 78
7 89
9 15
10 85
11 799
12 55
此处在8月(第8个月),变量为0。
bp <- barplot(V[,2], axes = FALSE)
axis(1, at = bp, labels=c("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"))
由于
答案 0 :(得分:2)
您需要在8月(第8个月)填写NA
。只需使用
bp <- barplot(append(V[,2], NA, 7), axes = FALSE)
axis(1, at = bp, labels=c("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"))
更一般地说,我会这样做:
x <- rep(NA, 12)
x[V$month] <- V$variable
bp <- barplot(x, axes = FALSE)
axis(1, at = bp, labels=c("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"))