Coloring under a specific area with another ggplot geom_area

I am desperately trying to put another geom area with ggplot in a specific area of my plot.

-I would like to have the area where I put the geom_rect colored under the first line of Probe "0.415cm/s". As seen in the picture but directly under the line

-Another geom_area with containment (gg) is not working. I got that from the Forum:

Conditional or arbitrary fill in density plots?

Following is my skript:

Data <- data.frame(
  X1 = c(0,	10,	20,	30,	40,	50,	60,	70,	80,	90,	100,0 ,	1.59,	3.18,	4.77,	6.36,	7.95,	9.54,	11.13,	12.72,	14.31,	15.9,	17.49,	19.08,	20.67,	22.26,	23.85,	25.44,	27.03,	28.62,	30.21,	31.8,	33.39,	34.98,	36.57,	38.16,	39.75,	41.34,	42.93,	44.52,	46.11,	47.7,	49.29,	50.88,	52.47,	54.06,	55.65,	57.24,	58.83,	60.42,	62.01,	63.6,	65.19,	66.7,	68.37,	69.96,	71.55,	73.14,	74.73,	76.32,	77.91,	79.5,	81.09,	82.68,	84.27,	85.86,	87.45,	89.04,	90.63,	92.22,	93.81,	95.4,	96.99,	98.58,	100),
  Y1 = c(367524,	367524,	325815,	234241.4,	335507,	843733.4,	877818.6,	208194.4,	208515,	268214.6,	263067.4, 327797,	327797,	366423,	370006.6,	367454.8,	368360,	384831.8,	364044.8,	344422,	353474.8,	396201.4,	352282.6,	351866.8,	305267.6,	211102.8,	217931.2,	230054.8,	239747,	235797.4,	224665.2,	230206.4,	225581.6,	224516.4,	236604.4,	218384.6,	336293.2,	640000.4,	652454.4,	935580.4,	872154.6,	717876,	876161.8,	882058.6,	863823.8,	898570.8,	879157.2,	907438.8,	935790.6,	756032.8,	256291.4,	198633.4,	208079.2,	210167.4,	207682.4,	205903.8,	208471.2,	206310.6,	204704.2,	203580.6,	207469.2,	204652.8,	195259.6,	199571.8,	242162.2,	254396.4,	261021,	318256.8,	315964,	292802,	270846.2,	286492,	232271.2,	292708.4,	267046.8),
  Probe = c("2.5cm/s",	"2.5cm/s",	"2.5cm/s",	"2.5cm/s",	"2.5cm/s",	"2.5cm/s",	"2.5cm/s",	"2.5cm/s",	"2.5cm/s",	"2.5cm/s",	"2.5cm/s", "0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s",	"0.415 cm/s"))

gg <- tibble(x = Data$X1, y = Data$Y1) %>% 
     mutate(variable = case_when(
         (x >= 60 & x <= 70) ~ "On",
         (x >= 20 & x <= 30) ~ "Off",
         TRUE ~ NA_character_))


n<-ggplot()+
  geom_area(data=Data[Data$Probe=="2.5cm/s",], mapping = aes(x=X1,y=Y1, fill=Probe),alpha=.6)+
  geom_area(data=Data[Data$Probe=="0.415 cm/s",], mapping = aes(x=X1,y=Y1, fill=Probe),alpha=.6)+
  geom_area(data = filter(gg, variable == 'Off'), fill ="green")+
geom_area(data = filter(gg, variable == 'On'), fill ="green")+
  scale_fill_manual(values=c("grey1","grey42"))+
  geom_point(aes(x = 62.01, y = 256291.40), shape=1, size=5, color="green")+
  geom_point(aes(x = 70.00, y = 208194.40), shape=1, size=5, color="green")+
  geom_segment(aes(x = 62.01    , y =0, xend = 62.01, yend = 256291.40), color="green", size = 1.0, linetype = "dashed")+
  geom_segment(aes(x = 70   , y =0, xend = 70, yend = 208194.40), color="green", size = 1.0, linetype = "dashed")+
  geom_rect(aes(xmin= 62.01, xmax= 70, ymin=0, ymax=188194.40),fill= "red", alpha=0.5)+  
  geom_point(aes(x = 20, y = 325815.00), shape=1, size=5, color="green")+
  geom_point(aes(x = 30, y = 234241.40), shape=1, size=5, color="green")+
  geom_segment(aes(x = 20   , y =0, xend = 20, yend = 325815.00), color="green", size = 1.0, linetype = "dashed")+
  geom_segment(aes(x = 30   , y =0, xend = 30, yend = 234241.40), color="green", size = 1.0, linetype = "dashed")+
  geom_rect(aes(xmin= 20, xmax= 30, ymin=0, ymax=188194.40),fill= "red", alpha=0.5)
 n


I hope someone can help me.
Thanks in advance

Is this what you mean?

library(tidyverse)
Data <- data.frame(
    X1 = c(0,   10, 20, 30, 40, 50, 60, 70, 80, 90, 100,0 , 1.59,   3.18,   4.77,   6.36,   7.95,   9.54,   11.13,  12.72,  14.31,  15.9,   17.49,  19.08,  20.67,  22.26,  23.85,  25.44,  27.03,  28.62,  30.21,  31.8,   33.39,  34.98,  36.57,  38.16,  39.75,  41.34,  42.93,  44.52,  46.11,  47.7,   49.29,  50.88,  52.47,  54.06,  55.65,  57.24,  58.83,  60.42,  62.01,  63.6,   65.19,  66.7,   68.37,  69.96,  71.55,  73.14,  74.73,  76.32,  77.91,  79.5,   81.09,  82.68,  84.27,  85.86,  87.45,  89.04,  90.63,  92.22,  93.81,  95.4,   96.99,  98.58,  100),
    Y1 = c(367524,  367524, 325815, 234241.4,   335507, 843733.4,   877818.6,   208194.4,   208515, 268214.6,   263067.4, 327797,   327797, 366423, 370006.6,   367454.8,   368360, 384831.8,   364044.8,   344422, 353474.8,   396201.4,   352282.6,   351866.8,   305267.6,   211102.8,   217931.2,   230054.8,   239747, 235797.4,   224665.2,   230206.4,   225581.6,   224516.4,   236604.4,   218384.6,   336293.2,   640000.4,   652454.4,   935580.4,   872154.6,   717876, 876161.8,   882058.6,   863823.8,   898570.8,   879157.2,   907438.8,   935790.6,   756032.8,   256291.4,   198633.4,   208079.2,   210167.4,   207682.4,   205903.8,   208471.2,   206310.6,   204704.2,   203580.6,   207469.2,   204652.8,   195259.6,   199571.8,   242162.2,   254396.4,   261021, 318256.8,   315964, 292802, 270846.2,   286492, 232271.2,   292708.4,   267046.8),
    Probe = c("2.5cm/s",    "2.5cm/s",  "2.5cm/s",  "2.5cm/s",  "2.5cm/s",  "2.5cm/s",  "2.5cm/s",  "2.5cm/s",  "2.5cm/s",  "2.5cm/s",  "2.5cm/s", "0.415 cm/s",    "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s",   "0.415 cm/s"))

gg <- tibble(x = Data$X1, y = Data$Y1) %>% 
    mutate(variable = case_when(
        (x >= 62.01 & x <= 70) ~ "On",
        (x >= 20 & x <= 30) ~ "Off",
        TRUE ~ NA_character_))


ggplot()+
    geom_area(data=Data[Data$Probe=="2.5cm/s",], mapping = aes(x=X1,y=Y1, fill=Probe),alpha=.6)+
    geom_area(data=Data[Data$Probe=="0.415 cm/s",], mapping = aes(x=X1,y=Y1, fill=Probe),alpha=.6)+
    geom_area(data = filter(gg, variable == 'Off'), mapping = aes(x=x,y=y), fill ="green")+
    geom_area(data = filter(gg, variable == 'On'), mapping = aes(x=x,y=y), fill ="green")+
    scale_fill_manual(values=c("grey1","grey42"))+
    geom_point(aes(x = 62.01, y = 256291.40), shape=1, size=5, color="green")+
    geom_point(aes(x = 70.00, y = 208194.40), shape=1, size=5, color="green")+
    geom_segment(aes(x = 62.01    , y =0, xend = 62.01, yend = 256291.40), color="green", size = 1.0, linetype = "dashed")+
    geom_segment(aes(x = 70   , y =0, xend = 70, yend = 208194.40), color="green", size = 1.0, linetype = "dashed")+
    geom_point(aes(x = 20, y = 325815.00), shape=1, size=5, color="green")+
    geom_point(aes(x = 30, y = 234241.40), shape=1, size=5, color="green")+
    geom_segment(aes(x = 20, y =0, xend = 20, yend = 325815.00), color="green", size = 1.0, linetype = "dashed")+
    geom_segment(aes(x = 30, y =0, xend = 30, yend = 234241.40), color="green", size = 1.0, linetype = "dashed")

Created on 2019-12-10 by the reprex package (v0.3.0.9000)

2 Likes

Thank you so much!
That is excactly what i was looking for.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.