R ggplot2警告缺少行/值并丢弃部分图

时间:2017-11-03 13:39:26

标签: r plot ggplot2 dplyr missing-data

我有一个R数据框,如下所示:

> glimpse(spottingIntensity)
Observations: 28
Variables: 5
$ nClassifications       <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,...
$ nPhotosClassified      <int> 45816, 25252, 12327, 5286, 2327, 1231, 713, 565, 447, 435, 318, 227, 192, 156,...
$ totalClassifiedPhotos  <int> 95781, 95781, 95781, 95781, 95781, 95781, 95781, 95781, 95781, 95781, 95781, 9...
$ proportionOfClassified <dbl> 4.783412e-01, 2.636431e-01, 1.286998e-01, 5.518840e-02, 2.429501e-02, 1.285224...
$ cumulativeProportions  <dbl> 0.4783412, 0.7419843, 0.8706842, 0.9258726, 0.9501676, 0.9630198, 0.9704639, 0...

其中,nClassificationsnPhotosClassified是数据,其他变量是派生的。

我使用以下内容用ggplot2绘制数据:

ggplot(data = spottingIntensity, mapping = aes(x = nClassifications, y = cumulativeProportions)) + 
    geom_col() + 
    geom_text(mapping = aes(label = nPhotosClassified), nudge_y = 0.03) + 
    scale_x_continuous(limits = c(NA, 10), 
                       breaks = seq.int(from =  1, to = 10, by = 1))

这给了我这些警告:

Warning messages:
1: Removed 18 rows containing missing values (position_stack). 
2: Removed 18 rows containing missing values (geom_text).

这个输出:

enter image description here

我在图中看到nClassifications = 10的列未显示,即使它的数据存在于原始数据框中。

我检查了数据框,我确实有一些&#34;缺少行&#34;适用于nClassifications = 24, 27, 30, 31,但不适用于nClassifications = 10

所以:

  1. 为什么nClassifications = 10出现的情节中没有栏?我该如何解决? (我期望一个高度类似于nClassifications = 9)的栏杆

  2. 我如何以编程方式&#34;填写/完成&#34;我的数据框是否有nClassifications = 24, 27, 30, 31的相应行?在这种情况下,nPhotosClassified <- 0用于这四个nClassifications。由此我可以推导出其他变量。

  3. dplyr / tidyr可以帮助1.和2.?或者还有另一种方式吗?谢谢!

    编辑:Ooooops,我之前粘贴了错误的代码段,现在它正确。

0 个答案:

没有答案
相关问题