如何在Barplot R中显示空数据 [英] How to show null data in barplot R

查看:33
本文介绍了如何在Barplot R中显示空数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个柱状图,其中x轴为月数,y轴为变量的值.该变量在几个月内可以为null.如何强制x轴始终显示12个月?

I want to draw a barplot with in axis x the number of months and in axis y the value of a variable. The variable can be null for some months. How can I coerce the axis x to always show the 12 months?

使用V时,数据帧如下:

With V a data frame like:

    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.

Here in August (month 8) the variable is 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"))

谢谢

推荐答案

您需要在8月(第8个月)填充 NA .只需使用

You need to pad an NA at August (month 8). Simply use

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

这篇关于如何在Barplot R中显示空数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆