当NA值存在时如何使用geom功能区?

时间:2017-03-22 22:02:26

标签: r ggplot2

我想做什么:

我想绘制几条曲线并填写它们之间的区域

问题

我使用geom_ribbon()填写区域。但它填写的不仅仅是我想要的。

错误填充白色区域:

enter image description here

我想在“OPDV”和“SDV”线之间填充白色(如图所示)。

代码和数据

我在上面的情节中使用了以下代码:

library(ggplot2)
library(dplyr) 

ggplot() +
  geom_ribbon(data = ddf,aes(ymin=BX,ymax=60, x=dv), fill="green") +
  geom_ribbon(data = ddf,aes(ymin=BX,ymax=s, x=SDV_1), fill="orange") +
  geom_ribbon(data = ddf,aes(ymin=BX,ymax=SDX_1, x=dv), fill="white") +

  geom_path(data = ddf,mapping = aes(x = CLDV_1, y = s), size=0.5)+
  geom_path(data = ddf,mapping = aes(x = OPDV_1, y = s), size=0.5) +

  geom_path(data = ddf,aes(x = SDV_1, y = s), size=0.5) +


  #geom_path(data = ddf,aes(x = dv, y = AX), size=0.5) +
  geom_path(data = ddf,aes(x = dv, y = BX), size=0.5) + 
  geom_path(data = ddf,aes(x = dv, y = SDX_1), size=0.5) +

  annotate(geom = "text", x = -0.8, y = 29, label = "OPDV",size = 3) +
  annotate(geom = "text", x = 1.5, y = 40, label = "SDV",size = 3) +

  labs(y = "Spacing (m)", x = "Relative Speed (Vf - Vl), m/s") +
  coord_cartesian(ylim = c(25, 50),
                  xlim = c(-2,3.2))  

我也尝试用以下内容替换第三行:

geom_ribbon(data = ddf %>% 
                 filter(dv>OPDV_1 & dv<SDV_1),
                          aes(ymin=BX,ymax=SDX_1, x=dv), fill="white")  

但是,由于OPDV_1SDV_1中的某些值为NA,因此根本不会填充白色。

以下是我的数据:

structure(list(BX = c(27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.4804347826087, 27.4295652173913, 
27.3786956521739, 27.3278260869565, 27.2769565217391, 27.2260869565217, 
27.1752173913043, 27.124347826087, 27.0734782608696, 27.0226086956522, 
26.9717391304348, 26.9208695652174, 26.87, 26.8191304347826, 
26.7682608695652, 26.7173913043478, 26.6665217391304, 26.615652173913, 
26.5647826086957, 26.5139130434783, 26.4630434782609, 26.4121739130435, 
26.3613043478261, 26.3104347826087, 26.2595652173913, 26.2086956521739, 
26.1578260869565, 26.1069565217391, 26.0560869565217, 26.0052173913043, 
25.954347826087, 25.9034782608696, 25.8526086956522, 25.8017391304348, 
25.7508695652174, 25.7), dv = c(3.2, 3.14347826086956, 3.08695652173913, 
3.03043478260869, 2.97391304347826, 2.91739130434782, 2.86086956521739, 
2.80434782608696, 2.74782608695652, 2.69130434782609, 2.63478260869565, 
2.57826086956522, 2.52173913043478, 2.46521739130435, 2.40869565217391, 
2.35217391304348, 2.29565217391304, 2.23913043478261, 2.18260869565217, 
2.12608695652174, 2.0695652173913, 2.01304347826087, 1.95652173913043, 
1.9, 1.84347826086956, 1.78695652173913, 1.7304347826087, 1.67391304347826, 
1.61739130434783, 1.56086956521739, 1.50434782608696, 1.44782608695652, 
1.39130434782609, 1.33478260869565, 1.27826086956522, 1.22173913043478, 
1.16521739130435, 1.10869565217391, 1.05217391304348, 0.995652173913044, 
0.939130434782609, 0.882608695652173, 0.826086956521738, 0.769565217391303, 
0.713043478260868, 0.656521739130433, 0.600000000000001, 0.543478260869566, 
0.486956521739131, 0.430434782608696, 0.373913043478261, 0.317391304347826, 
0.260869565217391, 0.204347826086956, 0.14782608695652, 0.0913043478260853, 
0.0347826086956502, -0.0217391304347814, -0.0782608695652165, 
-0.134782608695652, -0.191304347826087, -0.247826086956522, -0.304347826086957, 
-0.360869565217392, -0.417391304347827, -0.473913043478262, -0.530434782608694, 
-0.586956521739133, -0.643478260869564, -0.699999999999999, -0.756521739130434, 
-0.81304347826087, -0.869565217391305, -0.92608695652174, -0.982608695652175, 
-1.03913043478261, -1.09565217391305, -1.15217391304348, -1.20869565217392, 
-1.26521739130435, -1.32173913043478, -1.37826086956522, -1.43478260869565, 
-1.49130434782609, -1.54782608695652, -1.60434782608696, -1.66086956521739, 
-1.71739130434783, -1.77391304347826, -1.83043478260869, -1.88695652173913, 
-1.94347826086956, -2), s = 8:100, SDV_1 = c(NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
-0.0875, 0.0375, 0.1625, 0.2875, 0.4125, 0.5375, 0.6625, 0.7875, 
0.9125, 1.0375, 1.1625, 1.2875, 1.4125, 1.5375, 1.6625, 1.7875, 
1.9125, 2.0375, 2.1625, 2.2875, 2.4125, 2.5375, 2.6625, 2.7875, 
2.9125, 3.0375, 3.1625, 3.2875, 3.4125, 3.5375, 3.6625, 3.7875, 
3.9125, 4.0375, 4.1625, 4.2875, 4.4125, 4.53994565217391, 4.67130434782609, 
4.80266304347826, 4.93402173913043, 5.06538043478261, 5.19673913043478, 
5.32809782608696, 5.45945652173913, 5.5908152173913, 5.72217391304348, 
5.85353260869565, 5.98489130434783, 6.11625, 6.24760869565217, 
6.37896739130435, 6.51032608695652, 6.6416847826087, 6.77304347826087, 
6.90440217391304, 7.03576086956522, 7.16711956521739, 7.29847826086957, 
7.42983695652174, 7.56119565217391, 7.69255434782609, 7.82391304347826, 
7.95527173913043, 8.08663043478261, 8.21798913043478, 8.34934782608696, 
8.48070652173913, 8.6120652173913, 8.74342391304348, 8.87478260869565, 
9.00614130434783, 9.1375), SDX_1 = c(NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 31.5, 31.5, 31.5, 
31.5, 31.5, 31.5, 31.5, 31.4804347826087, 31.4295652173913, 31.3786956521739, 
31.3278260869565, 31.2769565217391, 31.2260869565217, 31.1752173913043, 
31.124347826087, 31.0734782608696, 31.0226086956522, 30.9717391304348, 
30.9208695652174, 30.87, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), CLDV_1 = c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 0.619176470588235, 0.646767058823529, 0.675703529411765, 
0.705985882352941, 0.737614117647059, 0.770588235294118, 0.804908235294118, 
0.840574117647059, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA), OPDV_1 = c(NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -0.619176470588235, 
-0.646767058823529, -0.675703529411765, -0.705985882352941, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA)), row.names = c(NA, -93L), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("BX", "dv", "s", "SDV_1", "SDX_1", 
"CLDV_1", "OPDV_1"))

如果ggplot2中有任何其他功能或我可以用来正确填写的扩展程序,请指导我。或者,如果可能,在这种情况下如何有效地使用geom_ribbon()

1 个答案:

答案 0 :(得分:1)

使用geom_polygon填充OPDV,SDX,SDV和BX曲线之间的白色区域。

创建数据以绘制白色多边形

library(tidyr)
# Change data to a long format
ddflong <- ddf %>% 
    gather(key, value, -dv, -s)

# Extract data for each polygon side 
sideOPDV <- ddflong %>% 
    filter(key =="OPDV_1" & !is.na(value)) %>% 
    transmute(x = value, y = s, key = key) %>% 
    arrange(y)

sideSDX <- ddflong %>% 
    filter(key =="SDX_1" & !is.na(value)) %>% 
    transmute(x = dv, y = value, key = key) %>% 
    arrange(x)

sideSDV <- ddflong %>% 
    filter(key =="SDV_1" & !is.na(value) & value < max(sideSDX$x)) %>% 
    transmute(x = value, y = s, key = key) %>% 
    arrange(desc(y))

sideBX <- ddflong %>% 
    filter(key == "BX" & dv > max(sideOPDV$x) & dv < min(sideSDV$x)) %>% 
    transmute(x = dv, y = value, key = key) %>% 
    arrange(desc(x))

# Combine all sides in one polygon
datapolygon <- rbind(sideOPDV, sideSDX, sideSDV, sideBX)

绘制情节

ggplot(data = ddf) +
    geom_ribbon(aes(ymin=BX,ymax=60, x=dv), fill="green") +
    geom_ribbon(aes(ymin=BX,ymax=s, x=SDV_1), fill="orange") +
    #### Here is the new instruction ####
    geom_polygon(data = datapolygon, aes(x = x, y =y), fill="white") +

    # Added colours to identify the lines
    geom_path(aes(x = CLDV_1, y = s), colour = "yellow")+
    geom_path(aes(x = OPDV_1, y = s), colour = "purple") +
    geom_path(aes(x = SDV_1, y = s), colour = "dark green") +
    geom_path(aes(x = dv, y = BX), colour = "blue") + 
    geom_path(aes(x = dv, y = SDX_1), colour = "red") +

    annotate(geom = "text", x = 1, y = 30, label = "CLDV", colour = "yellow") +
    annotate(geom = "text", x = -0.9, y = 29, label = "OPDV", colour = "purple") +
    annotate(geom = "text", x = 1.2, y = 40, label = "SDV", colour = "dark green") +
    annotate(geom = "text", x = -0.2, y = 26, label = "BX", colour = "blue") +
    annotate(geom = "text", x = -0.3, y = 32, label = "SDX", colour = "red") +

    labs(y = "Spacing (m)", x = "Relative Speed (Vf - Vl), m/s") +
    coord_cartesian(ylim = c(25, 50),
                    xlim = c(-2,3.2))  

plot with white polygon