【问题标题】:how to mark an area of a plot (geom_bar) which contains null data values ("NA")?如何标记包含空数据值(“NA”)的绘图区域(geom_bar)?
【发布时间】:2017-04-13 14:57:35
【问题描述】:

我正在构建一个函数来比较两组时间序列数据(降雨),以检查我们操作的雨量计的准确性 (QC)。当绘制包含记录间隙的数据文件时,可能会导致审阅者认为这些量具中的一个或多个记录不正确,从而扭曲了 QC 过程。

我想找到一种方法在图上绘制一个红色 x、一个圆圈或类似的东西,以提醒用户该特定电台在这些特定日期没有数据。这样用户可以清楚地看到数据丢失,并在审查过程中省略该特定记录部分。

使用这个df,allRN的任何想法

         date  usgs   noaa
1  2017-01-01  0.00 0.0000
2  2017-01-02  0.57 0.0906
3  2017-01-03  0.75 0.4410
4  2017-01-04  0.00 0.5790
5  2017-01-05  0.00 0.0000
6  2017-01-06  0.01 0.0000
7  2017-01-07  0.46     NA
8  2017-01-08  0.00     NA
9  2017-01-09  0.00 0.0000
10 2017-01-10  0.00 0.0000

还有这段代码:

  plotnames <- c(noaa = "Precip, Milton, DE, NOAA", usgs = "Precip,Ng45-02 Well, USGS")
  xdateaxis <- "3 days"  
  allRN <- allRN %>%
    gather(site, precip, -date) 

  ggplot(allRN, aes(x = date, y = precip)) + 
  labs (x = "", y = "Precipitation, inches") + 
  geom_bar(stat = "identity", position =     "dodge", na.rm = FALSE) + 
  facet_wrap(~site, nrow = 2, labeller =     as_labeller(plotnames)) + 
  scale_x_datetime(date_breaks = xdateaxis,     date_labels = "%m-%d") + 
  theme_bw() + theme(axis.text.x =  element_text(angle = 45, hjust = 1)) 

Link to example of plot output when data is missing

【问题讨论】:

  • 确保您始终添加一些示例数据,以便其他人可以使用
  • 对不起!菜鸟失误!!

标签: r ggplot2 dplyr geom-bar


【解决方案1】:

这是怎么回事?使用is.na(precip) 作为美学值,然后您可以制作点或条或任何您喜欢的东西,只要您设置结果比例,以便FALSENA 并且TRUE 是您的颜色或点形状等。

require(lubridate)
df.rain <- data_frame(date = mdy("01-01-2017") + 0:9,
                      usgs = c(0,.57,.75,0,0,.01,.46,0,0,0),
                      noaa = c(0,.0906,.4410,.5790,0,0,NA,NA, 0,0)) %>% 
  gather(site, precip, -date)

ggplot(df.rain, aes(date, precip)) + facet_grid(site~.) + 
  geom_col() +
  geom_tile(aes(x = date, y = -.1, 
                height = 0.1, width = 1, 
                fill = is.na(precip))) + 
  scale_fill_manual(values = c(NA, "red")) +
  scale_y_continuous(sec.axis = dup_axis(breaks = -0.1, 
                                         labels = "Missing\ndata:", 
                                         name = NULL)) + 
  guides(fill = F) +
  theme_bw()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    相关资源
    最近更新 更多