Home > Enterprise >  Subset in the graph in R
Subset in the graph in R

Time:01-06

I have created a plot according two groups: Healthy in blue and CAD in red (color = "group") with this code:

varia_plot <- df_sum %>% 
  filter(percent_power < 100) %>%
  ggscatter(x = "percent_power", y = "relVO2", color = "group", size = 0.5)  
  geom_smooth(aes(colour=group), method = "lm", formula = y ~ poly(x, 2))  
  geom_vline(aes(colour = c("red", "blue")), xintercept = c(VT1PPO_CAD, VT1PPO_healthy), linetype="solid")  
  geom_vline(aes(xintercept = percentPPO), linetype="dashed")  
  annotate("text", x=VT1PPO_CAD-2, y=1,   ##or bp_CAD if we use absVO2%
           label=paste0("VT1: ", round(VT1PPO_CAD, 0), "%"), angle=90, size = 4)   ##or bp_CAD if we use absVO2%
  annotate("text", x=VT1PPO_healthy-2, y=1,   ##or bp_CAD if we use absVO2%
           label=paste0("VT1: ", round(VT1PPO_healthy, 0), "%"), angle=90, size = 4)   ##or bp_CAD if we use absVO2%
  xlab("Percentage of PPO (%)")   
  ylab(expression(paste("V", O[2]," (L/min)")))  
  scale_x_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 25))  
  theme_classic()  
  theme(
    panel.background = element_rect(fill ="white", colour = "white"),
    axis.line = element_line(colour = "black"),
    legend.title = element_text(face = "bold"),
    legend.position = "none",
    strip.text = element_blank()
  )

And I gave this output: enter image description here

In two other columns (let say ColA and ColB), I have specific values (AAA and BBB for ColA and CCC and DDD forColB) only for CAD group. Healthy has NA value.

I would like to separate the CAD group into different subgroups according the ColA and ColB. In the end, I should have a graph for Healthy and 4 graphs for CAD (CAD-AAA, CAD-BBB, CAD-CCC, and CAD-DDD).

Thank you!

Here the data:

structure(list(percent_power = c(21.7391304347826, 34.7826086956522, 
47.8260869565217, 60.8695652173913, 73.9130434782609, 86.9565217391304, 
100, 19.2307692307692, 30.7692307692308, 42.3076923076923, 53.8461538461538, 
65.3846153846154, 76.9230769230769, 88.4615384615385, 100, 25, 
40, 55, 70, 85, 100, 13.1578947368421, 21.0526315789474, 28.9473684210526, 
36.8421052631579, 44.7368421052632, 52.6315789473684, 60.5263157894737, 
68.4210526315789, 76.3157894736842, 84.2105263157895, 92.1052631578947, 
100, 22.2222222222222, 33.3333333333333, 44.4444444444444, 55.5555555555556, 
66.6666666666667, 77.7777777777778, 88.8888888888889, 100, 15.3846153846154, 
23.0769230769231, 30.7692307692308, 38.4615384615385, 46.1538461538462, 
53.8461538461538, 61.5384615384615, 69.2307692307692, 76.9230769230769, 
84.6153846153846, 92.3076923076923, 100, 21.7391304347826, 34.7826086956522, 
47.8260869565217, 60.8695652173913, 73.9130434782609, 86.9565217391304, 
100, 29.4117647058824, 47.0588235294118, 64.7058823529412, 82.3529411764706, 
100, 14.2857142857143, 22.8571428571429, 31.4285714285714, 40, 
48.5714285714286, 57.1428571428571, 65.7142857142857, 74.2857142857143, 
82.8571428571429, 91.4285714285714, 100, 12.1951219512195, 19.5121951219512, 
26.8292682926829, 34.1463414634146, 41.4634146341463, 48.780487804878, 
56.0975609756098, 63.4146341463415, 70.7317073170732, 78.0487804878049, 
85.3658536585366, 92.6829268292683, 100, 22.2222222222222, 33.3333333333333, 
44.4444444444444, 55.5555555555556, 66.6666666666667, 77.7777777777778, 
88.8888888888889, 100, 41.6666666666667, 50, 58.3333333333333, 
66.6666666666667, 75, 83.3333333333333, 91.6666666666667, 100, 
25, 37.5, 50, 62.5, 75, 87.5, 100, 15.3846153846154, 23.0769230769231, 
30.7692307692308, 38.4615384615385, 46.1538461538462, 53.8461538461538, 
61.5384615384615, 69.2307692307692, 76.9230769230769, 84.6153846153846, 
92.3076923076923, 100, 33.3333333333333, 50, 66.6666666666667, 
83.3333333333333, 100, 19.2307692307692, 30.7692307692308, 42.3076923076923, 
53.8461538461538, 65.3846153846154, 76.9230769230769, 88.4615384615385, 
100, 19.2307692307692, 30.7692307692308, 42.3076923076923, 53.8461538461538, 
65.3846153846154, 76.9230769230769, 88.4615384615385, 100, 18.1818181818182, 
27.2727272727273, 36.3636363636364, 45.4545454545455, 54.5454545454545, 
63.6363636363636, 72.7272727272727, 81.8181818181818, 90.9090909090909, 
100, 15.3846153846154, 23.0769230769231, 30.7692307692308, 38.4615384615385, 
46.1538461538462, 53.8461538461538, 61.5384615384615, 69.2307692307692, 
76.9230769230769, 84.6153846153846, 92.3076923076923, 100, 17.2413793103448, 
27.5862068965517, 37.9310344827586, 48.2758620689655, 58.6206896551724, 
68.9655172413793, 79.3103448275862, 89.6551724137931, 100, 14.2857142857143, 
22.8571428571429, 31.4285714285714, 40, 48.5714285714286, 57.1428571428571, 
65.7142857142857, 74.2857142857143, 82.8571428571429, 91.4285714285714, 
100, 33.3333333333333, 44.4444444444444, 55.5555555555556, 66.6666666666667, 
77.7777777777778, 88.8888888888889, 100, 19.2307692307692, 30.7692307692308, 
42.3076923076923, 53.8461538461538, 65.3846153846154, 76.9230769230769, 
88.4615384615385, 100), relVO2 = c(8.79761904761905, 9.75892857142857, 
11.3244047619048, 12.8005952380952, 14.2738095238095, 16.0208333333333, 
17.8303571428571, 7.73391812865497, 8.75438596491228, 10, 11.5146198830409, 
13.4444444444444, 15.7485380116959, 16.9707602339181, 18.6959064327485, 
10.0941080196399, 11.7348608837971, 14.120294599018, 16.133387888707, 
18.494271685761, 20.6546644844517, 12.6214285714286, 13.5107142857143, 
15.4464285714286, 17.7714285714286, 18.6392857142857, 21.05, 
23.9857142857143, 25.5821428571429, 28, 29.15, 33.4892857142857, 
34.9071428571429, 9.63414634146342, 10.7479674796748, 12.0894308943089, 
14.2886178861789, 15.3414634146341, 16.2073170731707, 18.1178861788618, 
19.5569105691057, 8.04782608695652, 8.95, 9.71086956521739, 10.4108695652174, 
10.85, 12.4021739130435, 13.4108695652174, 13.8630434782609, 
14.754347826087, 15.4434782608696, 16.9152173913043, 17.7347826086956, 
10.0187265917603, 10.9456928838951, 14.3960674157303, 18.3895131086142, 
20.5945692883895, 23.5767790262172, 24.8408239700375, 10.7859281437126, 
12.997754491018, 13.8248502994012, 15.1309880239521, 17.5561377245509, 
9.56885456885457, 9.85521235521236, 12.3648648648649, 14.1248391248391, 
15.6981981981982, 17.3133848133848, 19.3854568854569, 21.5637065637066, 
23.9800514800515, 26.3384813384813, 27.5353925353925, 8.01307189542484, 
11.1797385620915, 12.8104575163399, 14.0653594771242, 16.8235294117647, 
19.1045751633987, 20.8267973856209, 22.1764705882353, 24.6274509803922, 
26.1176470588235, 28.0065359477124, 30.2320261437909, 32.3398692810458, 
10.1853015075377, 10.0942211055276, 10.8605527638191, 13.5175879396985, 
14.0577889447236, 15.7663316582915, 16.8121859296482, 18.8316582914573, 
10.6225425950197, 11.6186107470511, 13.5943643512451, 14.8623853211009, 
16.6022280471822, 17.8604193971166, 18.9449541284404, 20.0360419397117, 
9.98070987654321, 10.2546296296296, 11.4506172839506, 12.6041666666667, 
15.1697530864198, 16.4930555555556, 17.8240740740741, 7.9437984496124, 
8.01162790697674, 8.49031007751938, 9.43217054263566, 9.94573643410853, 
10.7771317829457, 11.1472868217054, 12.2093023255814, 12.6414728682171, 
13.9573643410853, 15.1608527131783, 15.8682170542636, 10.0854700854701, 
10.3205128205128, 11.7770655270655, 13.1623931623932, 14.1417378917379, 
10.2651515151515, 10.5492424242424, 12.2121212121212, 14.5189393939394, 
16.2765151515152, 18.9621212121212, 20.8068181818182, 24.6477272727273, 
11.109943977591, 11.7787114845938, 13.6239495798319, 14.6953781512605, 
16.3760504201681, 19.7794117647059, 21.4460784313725, 23.1757703081232, 
8.58091787439614, 9.04287439613527, 10.2928743961353, 11.1564009661836, 
12.7506038647343, 13.9492753623188, 14.8309178743961, 16.1382850241546, 
17.6388888888889, 18.9794685990338, 14.7027220630372, 17.621776504298, 
18.9971346704871, 22.5680515759312, 24.6776504297994, 27.3209169054441, 
30.6339541547278, 33.6926934097421, 36.1497134670487, 39.4734957020057, 
42.3244985673352, 44.6561604584527, 9.34447300771208, 9.77827763496144, 
11.9987146529563, 13.5539845758355, 15.7969151670951, 17.6188946015424, 
18.5957583547558, 20.4659383033419, 22.3200514138817, 9.3107476635514, 
9.26168224299065, 11.2056074766355, 11.2733644859813, 13.3598130841121, 
14.2453271028037, 14.8317757009346, 15.9883177570093, 17.6098130841122, 
18.4626168224299, 19.6285046728972, 21.0016420361248, 23.2717569786535, 
25.4022988505747, 29.8111658456486, 32.4466338259442, 35.5254515599343, 
38.3825944170772, 10.4560155239327, 11.3098318240621, 13.114489003881, 
14.8447606727038, 16.6364812419146, 18.5252263906856, 20.1811125485123, 
22.6131953428202), group = c("CAD", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", 
"CAD", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "Healthy", 
"Healthy", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "Healthy", "Healthy", "CAD", 
"CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", 
"CAD", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "CAD", "CAD", "CAD", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", 
"CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "CAD", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy", "Healthy", "Healthy", "Healthy", "Healthy", "Healthy", 
"Healthy"), new_id = c("12", "12", "12", "12", "12", "12", "12", 
"13", "13", "13", "13", "13", "13", "13", "13", "06", "06", "06", 
"06", "06", "06", "19", "19", "19", "19", "19", "19", "19", "19", 
"19", "19", "19", "19", "14", "14", "14", "14", "14", "14", "14", 
"14", "03", "03", "03", "03", "03", "03", "03", "03", "03", "03", 
"03", "03", "24", "24", "24", "24", "24", "24", "24", "22", "22", 
"22", "22", "22", "42", "42", "42", "42", "42", "42", "42", "42", 
"42", "42", "42", "18", "18", "18", "18", "18", "18", "18", "18", 
"18", "18", "18", "18", "18", "15", "15", "15", "15", "15", "15", 
"15", "15", "08", "08", "08", "08", "08", "08", "08", "08", "44", 
"44", "44", "44", "44", "44", "44", "33", "33", "33", "33", "33", 
"33", "33", "33", "33", "33", "33", "33", "40", "40", "40", "40", 
"40", "03", "03", "03", "03", "03", "03", "03", "03", "30", "30", 
"30", "30", "30", "30", "30", "30", "05", "05", "05", "05", "05", 
"05", "05", "05", "05", "05", "05", "05", "05", "05", "05", "05", 
"05", "05", "05", "05", "05", "05", "22", "22", "22", "22", "22", 
"22", "22", "22", "22", "28", "28", "28", "28", "28", "28", "28", 
"28", "28", "28", "28", "04", "04", "04", "04", "04", "04", "04", 
"21", "21", "21", "21", "21", "21", "21", "21"), procedure_type = c("CABG", 
"CABG", "CABG", "CABG", "CABG", "CABG", "CABG", "CABG", "CABG", 
"CABG", "CABG", "CABG", "CABG", "CABG", "CABG", 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, "CABG", "CABG", "CABG", "CABG", "CABG", 
"CABG", "CABG", "CABG", "CABG", "CABG", "CABG", "CABG", NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "PCI", "PCI", "PCI", 
"PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "PCI", "PCI", "PCI", 
"PCI", "PCI", "PCI", "PCI", "PCI", NA, NA, NA, NA, NA, NA, NA, 
NA, "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", 
"PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", 
"PCI", "PCI", "PCI", "PCI", "PCI", "PCI", NA, NA, NA, NA, NA, 
NA, NA, NA, "CABG", "CABG", "CABG", "CABG", "CABG", "CABG", "CABG", 
"CABG", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", 
"PCI", "PCI", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
"PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", "PCI", 
"CABG", "CABG", "CABG", "CABG", "CABG", "CABG", "CABG", "CABG", 
"CABG", "CABG", "CABG", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA), CAD_type = c("noMI", "noMI", "noMI", "noMI", 
"noMI", "noMI", "noMI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", 
"MI", 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, "MI", "MI", "MI", 
"MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "MI", "MI", "MI", "MI", 
"MI", "MI", "MI", "MI", "MI", "MI", "MI", NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, "noMI", "noMI", "noMI", "noMI", 
"noMI", "noMI", "noMI", "noMI", NA, NA, NA, NA, NA, NA, NA, NA, 
"MI", "MI", "MI", "MI", "MI", "MI", "MI", "noMI", "noMI", "noMI", 
"noMI", "noMI", "noMI", "noMI", "noMI", "noMI", "noMI", "noMI", 
"noMI", "MI", "MI", "MI", "MI", "MI", NA, NA, NA, NA, NA, NA, 
NA, NA, "MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", 
"MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "MI", "MI", "MI", "MI", 
"MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", "MI", 
"MI", "MI", "MI", "MI", "MI", NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -202L))

CodePudding user response:

As suggested in comments, create a column that has the values you want to color by:

library(ggplot2)
df_sum %>% 
  filter(percent_power < 100) %>% 
  mutate(
    real_group = paste(group, coalesce(CAD_type, ""), coalesce(procedure_type, ""), sep = " ")
  ) %>%
  ggplot(aes(x = percent_power, y = relVO2, color = real_group))  
  geom_point(size = 0.5)  
  geom_smooth(method = "lm", formula = y ~ poly(x, 2))  
  #geom_vline(aes(colour = c("red", "blue")), xintercept = c(VT1PPO_CAD, VT1PPO_healthy), linetype="solid")  
  #geom_vline(aes(xintercept = percentPPO), linetype="dashed")  
  # annotate("text", x=VT1PPO_CAD-2, y=1,   ##or bp_CAD if we use absVO2%
  #          label=paste0("VT1: ", round(VT1PPO_CAD, 0), "%"), angle=90, size = 4)   ##or bp_CAD if we use absVO2%
  # annotate("text", x=VT1PPO_healthy-2, y=1,   ##or bp_CAD if we use absVO2%
  #          label=paste0("VT1: ", round(VT1PPO_healthy, 0), "%"), angle=90, size = 4)   ##or bp_CAD if we use absVO2%
  xlab("Percentage of PPO (%)")   
  ylab(expression(paste("V", O[2]," (L/min)")))  
  scale_x_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 25))  
  theme_classic()  
  theme(
    panel.background = element_rect(fill ="white", colour = "white"),
    axis.line = element_line(colour = "black"),
    legend.title = element_text(face = "bold"),
    #legend.position = "none",
    strip.text = element_blank()
  )

enter image description here

  • Related