首页 文章

代码isn 't plotting level 0 of a factor in ggplot' s geom_bar()[关闭]

提问于
浏览
0

我的最终情节没有显示因子A的0水平 .

Headers 数据:

month  A           B
mayo   20           6.9
mayo   16          16.3
mayo   14          19.8
mayo   12          12.6
mayo    9          19.5
mayo    8          65.4
mayo   18          11.7
mayo   17          22.8
mayo   15          34.3
mayo   13          36.9
mayo   11          43.7
mayo   10          21.6
mayo   21          20.9
mayo    7           7.3
mayo   22          32.3
mayo   19          17.8
mayo    0          96.0

data.frame的结构:

'data.frame':   17 obs. of  3 variables:
 $ month          : chr  "mayo" "mayo" "mayo" "mayo" ...
 $ A         : Factor w/ 18 levels "0","7","8","9",..: 15 11 9 7 4 3 13 12 10 8 ...
 $ B: num  6.9 16.3 19.8 12.6 19.5 65.4 11.7 22.8 34.3 36.9 ...

我的ggplotting的R代码:

ggplot(data,aes(x = A, y = B))  +
        geom_bar(stat="identity",size=1.5,colour = "black",fill="green")

UPDATE

这是我的R代码的一个问题 . 0级包含在月份==“mayo”中,而不是月份==“abril”(包括在完整数据中) .

实际上,它没有显示0级:

ggplot(data[data$month=="abril",],aes(x = A, y = B))  +
            geom_bar(stat="identity",size=1.5,colour = "black",fill="green")

谢谢

1 回答

  • 1

    似乎为我工作

    data <- data.frame(month=rep('mayo',17),
               A=factor(c(20,16,14,12,9,8,18,17,15,13,11,10,21,7,22,19,0)),
               B=c(6.9,16.3,19.8,12.6,19.5,65.4,11.7,22.8,34.3,36.9,43.7,21.6,20.9,7.3,32.3,17.8,96.0),
               stringsAsFactors = F)
    
    ggplot(data,aes(x = A, y = B))  +
      geom_bar(stat="identity",size=1.5,colour = "black",fill="green")
    
    str(data)
    

    data.frame的结构

    'data.frame':   17 obs. of  3 variables:
     $ month: chr  "mayo" "mayo" "mayo" "mayo" ...
     $ A    : Factor w/ 17 levels "0","7","8","9",..: 15 11 9 7 4 3 13 12 10 8 ...
     $ B    : num  6.9 16.3 19.8 12.6 19.5 65.4 11.7 22.8 34.3 36.9 ...
    

    有一些关于data.frame的结构 . 在你发布的情况下,你有18个因子水平..对于17个观察?

相关问题