Reputation: 7147
I have some data which looks like:
# A tibble: 50 x 11
V1 V2 V3 V4 V5 V6 V7 V8 GRP ID OUTCOME
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int> <dbl>
1 0.667 0.539 0.373 -0.774 0.197 -0.0816 -0.0835 0.189 1 166 1
2 -0.436 -0.592 -0.686 -0.364 -0.0200 -0.0360 0.0285 -0.147 1 61 1
3 -0.631 -0.573 -0.705 -0.287 -0.0507 0.0474 0.0182 -0.0702 1 54 1
4 0.418 0.731 0.500 0.262 -0.0979 0.0353 0.0450 0.0156 1 240 0
5 1.03 0.281 -0.0649 -0.670 0.223 0.138 0.0336 0.162 1 179 1
6 -0.200 0.312 0.492 0.398 -0.0372 -0.0175 0.0101 0.0322 1 272 0
7 -0.595 -0.549 -0.606 -0.308 -0.0358 -0.0281 -0.0478 -0.0832 1 76 0
8 -0.289 -0.854 -1.01 0.169 -0.0207 -0.0149 -0.0742 -0.162 1 87 1
9 0.608 0.392 0.478 0.223 -0.0372 -0.0495 0.0101 0.0479 1 224 1
10 0.707 0.595 0.496 0.615 -0.0372 -0.0209 -0.149 0.0682 1 164 1
I can plot the data using:
df %>%
arrange(OUTCOME) %>%
tibble() %>%
pivot_longer(cols = c(V1:V8)) %>%
setNames(c("GRP", "ID", "OUTCOME", "name", "value")) %>% # not sure why but the column name for OUTCOME was changing too - OUTCOME[,"outcome"] so I had to add a "setNames()"
ggplot(aes(x = ID, y = value, fill = name)) +
geom_col(width = 1, alpha = 0.9)
Where I want to arrange by OUTCOME
and then plot such that the OUTCOME
= 0
comes first and the OUTCOME
= 1
comes second in the plot.
i.e. the x-axis corresponds to ID
's and therefore I want the first 1:26 observations (which are 0
- OUTCOME
) observations at the begining and the 27:50 ( which are 1
- OUTCOME
) observation at the end of the plot.
In order to make this more clear in the plot I want to add a geom_vline(xintercept = 26)
to the plot (which currently does not seem to do anything). Secondly, I want to add a geom_point
at the tip of each of the geom_cols
which are coloured according to the OUTCOME
variable - so each stacked geom_col
will have a point at the top.
Data:
df <- structure(list(V1 = c(0.666589915752411, -0.436222612857819,
-0.631442725658417, 0.418105125427246, 1.03165137767792, -0.19955663383007,
-0.595063030719757, -0.289468944072723, 0.608067691326141, 0.707050204277039,
-0.352968633174896, -0.348733305931091, -0.734579741954803, -0.407550632953644,
0.382651209831238, 0.317887604236603, 0.682134985923767, -1.3781418800354,
-0.399168610572815, -0.718308925628662, 0.867228746414185, 0.679950773715973,
0.839851379394531, -0.76373964548111, -1.42656266689301, -0.609997689723969,
-0.385522603988647, 0.439238548278809, -0.456471681594849, -0.280009210109711,
0.469753742218018, 0.318808495998383, -0.541835129261017, 0.842987537384033,
0.452548682689667, 0.535462975502014, -0.364355206489563, -1.42656266689301,
0.630698442459106, -0.44173675775528, -0.643441200256348, 0.685096621513367,
1.14054381847382, 0.370130002498627, -0.619462072849274, -0.845775902271271,
0.548840582370758, -0.477323114871979, -0.324755012989044, -0.781804263591766
), V2 = c(0.539071500301361, -0.59157657623291, -0.572662830352783,
0.730981111526489, 0.281135410070419, 0.311570674180984, -0.549109578132629,
-0.853675782680511, 0.392016798257828, 0.595141530036926, -0.566748976707458,
0.478970289230347, -0.689719200134277, -0.291370570659637, 0.328506886959076,
-1.00046491622925, 0.607915163040161, 0.167531609535217, -0.268897891044617,
-0.669840693473816, 0.352877289056778, 0.335686177015305, 0.4686459004879,
-0.120377041399479, 0.178255885839462, 0.242509573698044, 0.127220243215561,
0.723466992378235, 0.169776439666748, 0.311570674180984, 0.241861879825592,
-0.565791606903076, -0.596093893051147, 0.352877289056778, 0.755794286727905,
0.237963885068893, -0.610303223133087, 0.178255885839462, 0.604371428489685,
-0.566528797149658, -0.665839433670044, 0.572437882423401, -0.361080080270767,
0.698914647102356, -0.539785265922546, 0.217821598052979, 0.808691143989563,
0.50318056344986, -0.623627364635468, -0.344621360301971), V3 = c(0.373223125934601,
-0.686219215393066, -0.705432772636414, 0.500304698944092, -0.0648666396737099,
0.491708755493164, -0.60560667514801, -1.00564706325531, 0.477529734373093,
0.496099144220352, -0.680513858795166, -0.289659559726715, -0.865037560462952,
-0.3294717669487, 0.313058227300644, -0.659056186676025, 0.355539441108704,
0.356298297643661, -0.742002785205841, -0.782470107078552, 0.462770104408264,
0.524555385112762, 0.507511615753174, -0.400415688753128, 0.38832151889801,
0.54375559091568, 0.348951697349548, 0.423646926879883, -0.22981920838356,
0.472731471061707, 0.31219682097435, -0.381594121456146, -0.723009943962097,
0.391745924949646, 0.414667755365372, 0.31219682097435, -0.779489040374756,
0.38832151889801, 0.480808824300766, -0.666897296905518, -0.755469918251038,
0.401484251022339, -0.543907880783081, 0.344876348972321, -0.528214573860168,
-0.269392281770706, 0.56480598449707, 0.621908187866211, -0.531825602054596,
-0.434289932250977), V4 = c(-0.773811459541321, -0.363897234201431,
-0.287408024072647, 0.261809468269348, -0.670326471328735, 0.397776305675507,
-0.307681560516357, 0.168681144714355, 0.223264053463936, 0.614758968353271,
-0.338626772165298, -0.846252083778381, 0.11482846736908, -0.345179617404938,
-1.05273842811584, -0.350423514842987, 0.196808382868767, 0.00778263807296753,
0.189556911587715, 0.123728767037392, 0.583511531352997, 0.52887761592865,
0.522512197494507, -0.343501180410385, 0.257596492767334, 0.581979811191559,
-0.991406679153442, -0.863794207572937, -0.758265018463135, 0.374924182891846,
-1.05103802680969, -0.504420638084412, 0.00401294603943825, 0.335065960884094,
-0.823703289031982, -1.05103802680969, -0.353600144386292, 0.257596492767334,
0.229928761720657, -0.338579297065735, -0.257122099399567, -0.802706837654114,
0.196976840496063, -0.947062849998474, -0.328546792268753, -0.37180632352829,
0.473592817783356, 0.396025836467743, -0.382713079452515, -0.403718531131744
), V5 = c(0.197130978107452, -0.0200146716088057, -0.0507026240229607,
-0.0978699624538422, 0.222558438777924, -0.0371929332613945,
-0.0357776693999767, -0.0207225400954485, -0.0371929332613945,
-0.0371929332613945, -0.0357776693999767, -0.102090179920197,
0.032932948321104, -0.0899124220013618, -0.102090179920197, 0.0611645467579365,
0.0739013180136681, -0.0371929332613945, -0.0207225400954485,
0.0266784615814686, -0.0371929332613945, -0.0371929332613945,
-0.0978699624538422, -0.0507026240229607, -0.0371929332613945,
-0.0371929332613945, -0.102090179920197, 0.222558438777924, -0.102090179920197,
-0.0371929332613945, -0.102090179920197, -0.0442905239760876,
-0.0356474928557873, -0.0371929332613945, 0.197130978107452,
-0.102090179920197, -0.0507026240229607, -0.0371929332613945,
0.0739013180136681, -0.0357776693999767, 0.0674190372228622,
0.222558438777924, -0.0356474928557873, 0.197130978107452, -0.0357776693999767,
0.193563550710678, 0.0484738424420357, 0.0484738424420357, -0.0357776693999767,
0.0674190372228622), V6 = c(-0.0815747752785683, -0.0359503589570522,
0.0473587699234486, 0.0353398211300373, 0.137635827064514, -0.017513670027256,
-0.0280688628554344, -0.0148527026176453, -0.0495327338576317,
-0.0209453999996185, -0.0280688628554344, -0.0714860409498215,
0.0438234098255634, 0.120613776147366, -0.0714860409498215, -0.0342447310686111,
0.0836121588945389, -0.0538526326417923, -0.0134977530688047,
-0.0259735006839037, -0.017513670027256, -0.017513670027256,
0.0353398211300373, 0.0394904688000679, -0.0380047224462032,
-0.0380047224462032, -0.0495327338576317, 0.174676224589348,
-0.0758059397339821, -0.0380047224462032, -0.0495327338576317,
-0.0714860409498215, 0.0473587699234486, -0.017513670027256,
-0.103528082370758, -0.0495327338576317, 0.0473587699234486,
-0.0380047224462032, 0.0894022956490517, -0.0280688628554344,
0.0527884140610695, 0.174676224589348, 0.0250600017607212, -0.103528082370758,
-0.0280688628554344, -0.0359503589570522, -0.0529874339699745,
-0.073478490114212, -0.0263632386922836, 0.0527884140610695),
V7 = c(-0.0834691673517227, 0.0285118855535984, 0.0181918852031231,
0.0450421050190926, 0.0335643924772739, 0.0101256892085075,
-0.0478062480688095, -0.0741845071315765, 0.0101256892085075,
-0.148965746164322, -0.0173042453825474, 0.0335643924772739,
0.0111529966816306, 0.0335643924772739, -0.241482585668564,
0.0323374196887016, 0.0374831557273865, 0.00208079349249601,
0.0306694488972425, -0.0391291491687298, 0.0101256892085075,
0.0101256892085075, 0.0450421050190926, 0.0188414938747883,
0.00208079349249601, 0.0101256892085075, -0.0643396899104118,
0.0936737284064293, 0.0335643924772739, 0.0101256892085075,
-0.0643396899104118, 0.0323374196887016, 0.0168080646544695,
0.0101256892085075, 0.110289998352528, -0.0643396899104118,
0.0285118855535984, 0.00208079349249601, 0.0450421050190926,
-0.0593610778450966, 0.016591826453805, 0.110289998352528,
0.0382283963263035, -0.241482585668564, -0.0384819433093071,
0.0229429267346859, 0.0101256892085075, -0.193015351891518,
-0.0816542878746986, 0.0229429267346859), V8 = c(0.188536351546645,
-0.14695412479341, -0.0702244397252798, 0.015599824488163,
0.162356040906161, 0.0322057278826833, -0.0832092929631472,
-0.162452442571521, 0.0479177413508296, 0.0682498225942254,
0.153729286044836, -0.243203143589199, 0.149779425002635,
-0.242116670124233, 0.109018758870661, -0.142883030232042,
-0.245007876306772, -0.0900559583678842, -0.247359684668481,
0.148495792876929, -0.0406780429184437, 0.00872406736016273,
0.138334958814085, -0.0638183308765292, -0.00990268588066101,
-0.0324675692245364, -0.114242037758231, -0.0935285966843367,
0.0216120602563024, 0.0322057278826833, 0.0343138063326478,
-0.0388489956967533, 0.0989119447767735, -0.0688016824424267,
0.166093233972788, -0.0274974387139082, 0.0373926516622305,
-0.00990268588066101, 0.140418875962496, -0.115373022854328,
-0.0672494322061539, 0.0669955778867006, 0.0579314436763525,
-0.194228617474437, -0.13398567494005, 0.117101473733783,
0.0771859539672732, 0.150191807188094, -0.1857496839948,
0.0725541273131967), GRP = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), ID = c(166L,
61L, 54L, 240L, 179L, 272L, 76L, 87L, 224L, 164L, 13L, 125L,
14L, 41L, 147L, 64L, 33L, 260L, 95L, 10L, 219L, 234L, 243L,
53L, 261L, 279L, 123L, 177L, 127L, 269L, 142L, 63L, 18L,
214L, 168L, 141L, 20L, 263L, 191L, 75L, 3L, 182L, 31L, 171L,
74L, 170L, 249L, 154L, 67L, 4L), OUTCOME = c(1, 1, 1, 0,
1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1,
1, 1, 0, 1, 1, 1, 1, 0)), class = "data.frame", row.names = c(NA,
-50L))
EDIT:
(Excuse my poor MS Paint skills) - I was hoping for something like the following - where there are geom_points
coloured at the begining (corresponding to 0) and then geom_points
at the end (corresponding to 1). One point for each geom_col
. (The geom_points
do not need to lie on the same line as in the MS Paint version - I am happy also if they sit on top of each geom_col
).
Upvotes: 1
Views: 95
Reputation: 24888
Understanding the biological problem really helped. How about ordering the individuals by value first and then plotting the outcome as the point?
df %>%
arrange(OUTCOME) %>%
tibble() %>%
pivot_longer(cols = c(V1:V8)) %>%
ggplot(aes(x = reorder(factor(ID,unique(ID)),value, sum), y = value)) +
geom_point(aes(y = 3, color = as.factor(OUTCOME))) +
scale_color_manual(values = c("red","black"), labels = c("Dead","Alive")) +
geom_col(aes(fill = name),width = 1, alpha = 0.9) + labs(x = "ID", color = "Outcome") +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank())
Upvotes: 2