代码不是在ggplot的geom_bar()中绘制因子的0级

时间:2017-03-28 11:59:19

标签: r ggplot2 geom-bar

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

标题数据:

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

更新

我的R代码出了问题。 0级包含在月份==" mayo&#34 ;,而不是月份==" abril" (包括在完整数据中)。

确实,它没有显示0级:

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

由于

1 个答案:

答案 0 :(得分: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次观察?