1

Here is a dput sample of my data ...

SKOV<-as.data.frame(structure(list(sample_id = c(147, 167, 18, 131, 17, 222, 270, 
19, 173, 56, 265, 151, 250, 273, 17, 61, 324, 279, 161, 144, 
312, 97, 88, 167, 112, 146, 30, 17, 52, 99, 188, 135, 184, 277, 
93, 305, 37, 264, 253, 74, 299, 176, 93, 24, 162, 7, 209, 259, 
85, 231), cluster = structure(c(3L, 3L, 1L, 3L, 3L, 2L, 1L, 1L, 
3L, 3L, 1L, 3L, 2L, 1L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 3L, 3L, 3L, 
1L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 1L, 3L, 2L, 2L, 1L, 2L, 3L, 
2L, 3L, 3L, 1L, 2L, 1L, 1L, 2L, 3L, 3L), .Label = c("1", "2", 
"3"), class = "factor"), PC1 = c(3.77541554972203, 1.45135092275582, 
-2.08151488022046, 1.67392278661776, -0.868667705111095, 0.381346152049824, 
-4.51990813842073, -4.33106175029972, 4.09484115180022, 1.46878082673118, 
-3.91009236070008, 2.29855909444833, 1.28996975316864, -4.33482864589876, 
-0.868667705111095, 1.71386877911134, 0.542339094466148, -5.42446660572953, 
2.07724803679489, 3.1482373274481, 0.319519012725789, 1.35932545954681, 
0.515976962035749, 1.45135092275582, -2.2956688398855, 3.12130887968216, 
0.957815542171644, -0.868667705111095, 2.44038404221629, -3.73532585246298, 
3.60298231127924, 1.76697731520925, 0.107335593841895, -4.24926391802412, 
2.37822851276455, 0.863188301906724, 0.633358640568618, -4.49426173195687, 
0.657329751847109, 1.07849849754947, 0.949226371513706, 2.20251690907612, 
2.37822851276455, -5.27188001219925, 0.883552693512362, -3.65471472570778, 
-4.17396534181865, 0.740413932072948, 2.35215180710275, 2.95723653631208
), PC2 = c(2.9821460311606, 1.84547428893804, -0.110154674255345, 
0.94494220934088, 1.77529975625982, -1.75627062956882, 0.455969541574471, 
1.01232365128454, 5.22885669105582, 0.614756183817838, 0.53924434139561, 
0.824937608066585, -2.58029550589492, 0.599258675742853, 1.77529975625982, 
-1.51359310918018, -2.94834599434092, 1.06948580574065, -1.53223407015238, 
2.15612285827502, -3.11118425549662, 1.35847952064022, 1.52107198979851, 
1.84547428893804, 0.600591536702616, 0.873482585063045, -2.66401802898751, 
1.77529975625982, 1.72968551305468, 1.57607478071461, 4.6737233499792, 
0.407336067372946, 2.67228082439395, 0.116730668123481, 3.76755528568674, 
-3.38651333356804, -1.44641388322638, 0.154407055925795, -2.43105409429423, 
0.644629065490083, -1.74893772151854, 2.74702223077597, 3.76755528568674, 
1.36129565011855, -0.771286443151456, 1.26640510339257, -0.24592726728564, 
-2.12450533889367, 1.59309393261233, 0.536480763301413), PC3 = c(-1.54368524007348, 
2.43402556007619, -0.0450967184646642, 0.0410426039829931, -0.848801725782723, 
0.38804666076165, -1.53725160414506, 0.00144436967650244, -0.219129975234483, 
-0.549564648310951, 0.404446222294109, -0.2024943392322, -0.151863586122569, 
0.595219147562226, -0.848801725782723, -0.546704676736899, 0.603107536227402, 
-3.84773521184464, -1.12569943452765, -0.784183732377998, 0.954584210395496, 
1.03620198922004, 2.88552053445592, 2.43402556007619, -6.37932899691359, 
-0.440575910698521, 0.577066781163853, -0.848801725782723, 0.0762964729013986, 
-0.197383547463127, -0.122216505063875, -0.152129782123152, 5.9139111613348, 
0.419059172110045, 0.538964366314296, 0.288559910935336, 0.573620970938223, 
-1.43282961184911, 0.273788799449918, 1.17945429175878, -0.939838485321215, 
0.769679221867679, 0.538964366314296, 0.154922166837421, 0.640999296534156, 
-0.213535320971047, 0.487374487637418, 0.530768949185841, 0.682768810474348, 
-2.65577976399495), PC4 = c(0.513667495685775, -1.61889899346387, 
-0.00199956789317688, 0.0852133725033396, 0.200110415721805, 
-0.54476643944572, -0.665890345796119, 0.692033116930379, 0.68095691640909, 
0.0715306855842227, 1.56182228831717, 0.836306992758303, -0.213466309266933, 
1.25649126097911, 0.200110415721805, 0.329995819652711, -0.673049170161758, 
-7.16504510762261, -0.00823529898926304, 0.636191153685101, -0.112239279821607, 
-0.668345916270364, -2.22187934484341, -1.61889899346387, -5.79832117606908, 
0.679388729196689, -0.565063309411897, 0.200110415721805, 0.277981223741711, 
0.240500293392891, 0.424228959808187, 0.188958927083737, -4.88485900638901, 
1.39045390873317, 0.044539479926183, -0.39937436237614, -0.127318966653892, 
-3.92777799888502, 0.106028414733035, -1.35733137106087, 0.604053687622463, 
-0.283098175950597, 0.044539479926183, 0.699593792047251, -0.234545309910987, 
0.563077866073106, 1.41270865461162, 0.351700547445558, -0.417798927400163, 
1.04741309069348)), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame")))

I'm trying to use geom_rec to shade regions of line plots with facets ... However, the best i got is this my effort so far.

and here is how i arrived at that image

library(tidyverse)
SKOV_2<-SKOV%>%
    pivot_longer(3:4, values_to="conc", names_to="s_elements")%>%mutate(s_elements=as.factor(s_elements))

SKOV_2
  ggplot(data=SKOV_2, aes(x=sample_id,y=conc ,colour = factor(s_elements))) + # 
  geom_rect(aes(xmin = sample_id, xmax = dplyr::lead(sample_id), ymin = min(conc), ymax = Inf, fill = cluster), 
            alpha = 0.5) +
  geom_line(show.legend = FALSE, lwd = 0.8) +
     facet_wrap(~ s_elements, scales = "free") +
  
  coord_flip() +
scale_x_reverse()

What i hope to do is similar to this imagesimilar to this image for each facet.

I've looked at this question and this other question.... but they are somewhat far from what i'm trying to do. Any guidiance will be appreciated. The actually dataset is to large to be shared as dput but i can provide it if required. The main idea is to use the "clust" column for shading.

Hammao
  • 801
  • 1
  • 9
  • 28
  • your example is not reproducible: there are variables you use in your plotting code that aren't in your example data frame, including the facet variable itself. – Allan Cameron Aug 08 '20 at 18:17
  • @AllanCameron, this for pointing this out... i've updated the code and data... earlier, i shared to code for used for the entire dataset, it should work now. – Hammao Aug 08 '20 at 18:44

1 Answers1

2

A couple of things to note. If you are going to use lead, you need to make sure that the sample ids are ordered within their groups. Secondly, to make background rectangles work, you probably want to remove the outlines. Thirdly, you probably need to drop the alpha right down;

library(tidyverse)

SKOV %>%
    pivot_longer(3:4, values_to="conc", names_to="s_elements") %>%
    mutate(s_elements = as.factor(s_elements)) %>%
    group_by(s_elements) %>%
    arrange(sample_id, by_group = TRUE) %>%
    mutate(sample_id_lead = dplyr::lead(sample_id)) %>%
    ggplot(aes(x  =sample_id, y = conc ,colour = factor(s_elements))) +
    geom_rect(aes(xmin = sample_id, xmax = sample_id_lead, 
                  ymin = min(conc), ymax = Inf, fill = cluster), 
              alpha = 0.1, color = NA) +
    scale_fill_manual(values = c("red", "blue", "green")) +
    geom_line(show.legend = FALSE, lwd = 0.8) +
    facet_wrap(~ s_elements, scales = "free") +
    coord_flip() +
    scale_x_reverse()

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87