如何标记包含空数据值 ("NA") 的绘图区域 (geom_bar)?
how to mark an area of a plot (geom_bar) which contains null data values ("NA")?
我正在构建一个函数来比较两组时间序列数据(降雨量),以检查我们操作的雨量计的准确性 (QC)。当绘制包含记录间隙的数据文件时,可能会使审阅者认为这些量具中的一个或多个记录不正确,从而扭曲 QC 过程。
我想找到一种方法来在图表上绘制红色 x、圆圈或类似的东西,以提醒用户该特定站点在那些特定日期没有 data.This 方式用户可以清楚地看到数据丢失,并在审查过程中忽略记录的特定部分。
任何使用此 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
这个怎么样?使用 is.na(precip)
作为审美价值,然后您可以制作点或条形图或任何您喜欢的东西,只要您设置结果比例以使 FALSE
为 NA
和 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()
我正在构建一个函数来比较两组时间序列数据(降雨量),以检查我们操作的雨量计的准确性 (QC)。当绘制包含记录间隙的数据文件时,可能会使审阅者认为这些量具中的一个或多个记录不正确,从而扭曲 QC 过程。
我想找到一种方法来在图表上绘制红色 x、圆圈或类似的东西,以提醒用户该特定站点在那些特定日期没有 data.This 方式用户可以清楚地看到数据丢失,并在审查过程中忽略记录的特定部分。
任何使用此 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
这个怎么样?使用 is.na(precip)
作为审美价值,然后您可以制作点或条形图或任何您喜欢的东西,只要您设置结果比例以使 FALSE
为 NA
和 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()