Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

r - how to make stacked barplot with defined horizontal borders for each bar

I have data that i want to show in barplot in a way that i could'nt figure out how. Hope you can help me with this!

My table consists of 4 columns: cluster (0:6), IgG_Status (mild_high, mild_low,Severe_High), patient (1-16) and value (normalized value per each cluster). These are the lines of code i'm using now to create a barplot of the sum of values for each cluster, divided to IgG_Status (dodged style).

ggplot(mat, aes(x= cluster, fill= IgG_status, group=IgG_status)) + geom_bar(aes(weight = normalizedppstatus), position = "dodge") 

I want to add to this graph horizontal lines that describe each patient contribution to the each bar. i managed to do it using facet_grid but it changed the whole style of the figure so it's not good for me. code for using facet_grid below:

ggplot(mat, aes(x= IgG_status , y= normalizedppstatus, fill= IgG_status)) + geom_col(color = "black") + facet_grid(~cluster) +
theme(axis.text.x = element_text(angle=30))

mat structure:

structure(list(cluster = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("0", "1", "2", "3", "4"), class = "factor"), IgG_status = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("mild_high", "mild_low", "Severe_High" ), class = "factor"), patient = structure(c(5L, 11L, 16L, 4L, 6L, 7L, 8L, 12L, 2L, 3L, 9L, 10L, 13L, 14L, 17L, 5L, 11L, 16L, 4L, 6L, 7L, 8L, 12L, 2L, 3L, 9L, 10L, 13L, 14L, 17L, 5L, 11L, 16L, 4L, 6L, 7L, 8L, 12L, 2L, 3L, 9L, 10L, 13L, 14L, 17L, 5L, 11L, 16L, 4L, 6L, 7L, 8L, 12L, 2L, 3L, 9L, 10L, 13L, 14L, 17L, 5L, 16L, 4L, 6L, 7L, 8L, 2L, 3L, 9L, 13L, 14L, 17L), .Label = c("Contact3", "CoV1", "CoV2", "CoV3", "CoV4", "CoV5", "CoV6", "CoV7", "CoV8", "CoV9", "CoV10", "CoV11", "CoV12", "CoV13", "CoV14", "CoV15", "CoV16"), class = "factor"), Freq = c(176L, 164L, 345L, 505L, 277L, 1421L, 679L, 104L, 235L, 933L, 692L, 682L, 133L, 1278L, 330L, 420L, 166L, 288L, 231L, 701L, 1105L, 431L, 506L, 180L, 814L, 410L, 363L, 283L, 182L, 268L, 657L, 155L, 82L, 872L, 385L, 277L, 23L, 298L, 87L, 128L, 469L, 640L, 197L, 148L, 73L, 688L, 220L, 51L, 263L, 456L, 312L, 693L, 303L, 120L, 400L, 373L, 35L, 62L, 170L, 166L, 7L, 530L, 5L, 1L, 80L, 876L, 19L, 7L, 2L, 4L, 15L, 153L), percentperstatus = c(0.0445682451253482, 0.041529501139529, 0.0873638895923018, 0.0467419474268789, 0.0256386523509811, 0.131525360977416, 0.0628470936690115, 0.00962606442058497, 0.0233807581335191, 0.0928265844194607, 0.0688488707591284, 0.0678539448811064, 0.0132325141776938, 0.127151527211223, 0.0328325539747289, 0.106356039503672, 0.0420359584704989, 0.0729298556596607, 0.021380970011107, 0.0648833765272121, 0.102276934468715, 0.0398926323583858, 0.0468345057386153, 0.0179086658043976, 0.0809869664709979, 0.0407919609989056, 0.0361158093722018, 0.0281564023480251, 0.018107650980002, 0.0266640135309919, 0.166371233223601, 0.0392504431501646, 0.0207647505697645, 0.0807108478341355, 0.0356349500185117, 0.0256386523509811, 0.00212884116993706, 0.0275823768974454, 0.00865585513879216, 0.0127350512386827, 0.0466620236792359, 0.0636752561934136, 0.0196000397970351, 0.0147249029947269, 0.00726295890956124, 0.174221321853634, 0.0557103064066852, 0.0129146619397316, 0.0243428359866716, 0.0422065901517956, 0.0288781932617549, 0.064142910033321, 0.0280451684561274, 0.011939110536265, 0.0397970351208835, 0.0371107352502239, 0.00348224057307731, 0.00616854044373694, 0.0169137399263755, 0.0165157695751667, 0.00177260065839453, 0.134211192707014, 0.00046279155868197, 9.25583117363939e-05, 0.00740466493891151, 0.0810810810810811, 0.00189035916824197, 0.000696448114615461, 0.000198985175604417, 0.000397970351208835, 0.00149238881703313, 0.0152223659337379), normalizedppstatus = c(0.0508788793021933, 0.0474098648043165, 0.0997341668139585, 0.0533603666644943, 0.0292689535961681, 0.150148675307419, 0.0717459187429537, 0.0109890656101137, 0.0266913531758627, 0.105970351119489, 0.0785975165859447, 0.077461714323142, 0.0151061700952755, 0.145155529186181, 0.0374814746724881, 0.142698157688979, 0.0563997480389775, 0.0978501652724429, 0.0286868996290798, 0.0870541845886795, 0.137225212511399, 0.0535240421650797, 0.0628379706160796, 0.0240281006032283, 0.10866041050571, 0.0547306735962423, 0.0484566695498438, 0.0377775137261868, 0.0242950794988198, 0.0357751720092511, 0.291165142146192, 0.0686919285124198, 0.0363402460517317, 0.141251495387918, 0.0623644790416841, 0.0448700277780429, 0.00372567017651619, 0.048271726634862, 0.0151485520846987, 0.0222875249062233, 0.0816628842267089, 0.111437624531117, 0.0343018938009843, 0.0257699506728207, 0.0127108540480805, 0.309787838022592, 0.0990600644839683, 0.0229639240394654, 0.0432846821009943, 0.0750487263804311, 0.0513491285760844, 0.114054314433418, 0.0498679037133128, 0.0212292686198854, 0.0707642287329512, 0.065987643293477, 0.00619187001413323, 0.0109684554536074, 0.0300747972115043, 0.0293671549241748, 0.00723736731183195, 0.547972096467276, 0.00188953585407682, 0.000377907170815364, 0.0302325736652291, 0.331046681634259, 0.00771816460016861, 0.00284353432637791, 0.000812438378965117, 0.00162487675793023, 0.00609328784223838, 0.0621515359908314)), row.names = c(5L, 11L, 16L, 21L, 23L, 24L, 25L, 29L, 36L, 37L, 43L, 44L, 47L, 48L, 51L, 56L, 62L, 67L, 72L, 74L, 75L, 76L, 80L, 87L, 88L, 94L, 95L, 98L, 99L, 102L, 107L, 113L, 118L, 123L, 125L, 126L, 127L, 131L, 138L, 139L, 145L, 146L, 149L, 150L, 153L, 158L, 164L, 169L, 174L, 176L, 177L, 178L, 182L, 189L, 190L, 196L, 197L, 200L, 201L, 204L, 209L, 220L, 225L, 227L, 228L, 229L, 240L, 241L, 247L, 251L, 252L, 255L), class = "data.frame")

Thanks!

question from:https://stackoverflow.com/questions/66062111/stacked-barplot-with-defined-horizontal-borders-for-each-bar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
library(tidyverse)
ggplot(mat, aes(x= cluster, fill= IgG_status, group=IgG_status)) + 
  geom_col(aes(y = normalizedppstatus), color = "white", position = "dodge")

By using geom_col, ggplot plots each patient as a bar, rather than calculating one grouped total to display with geom_bar. Adding a white border with the color aesthetic seems like the simplest way to separate the patients visually.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...