1

I have a data with 3 different disease conditions and several outcomes of them. Diseases are in the first column and outcomes are named from second column on. Please see the data in the attachment.

Disease A   B   C   D   E   F   G   H
X   0.001527882 0.003459413 0.000648574 0.002127627 0.000847073 0.002628663 0.00000156  0.001697012
X   0.001848781 0.003641997 0.000876108 0.0024079   0.001061678 0.00253612  0.00000191  0.001761288
X   0.004304498 0.005887634 0.003197471 0.003581047 0.002125073 0.003125686 0.000025    0.001974246
X   0.006675181 0.00784345  0.004085157 0.005653283 0.003118116 0.001897106 0.000011    0.004055166
X   0.006206229 0.007407223 0.003775627 0.005398474 0.002903612 0.001901196 0.0000138   0.003867669
X   0.008291067 0.010172375 0.005316099 0.007294589 0.003807516 0.002711993 0.00000431  0.004335257
X   0.00405819  0.006540146 0.002686623 0.004762546 0.00163977  0.003309515 0.00000789  0.001957311
X   0.0067902   0.010219971 0.004727677 0.0070145   0.002995965 0.005784826 0   0.003331767
X   0.005495986 0.008415487 0.003807673 0.00552374  0.002361458 0.005504102 0.000000805 0.002746723
X   0.005209265 0.008041805 0.003572746 0.005247395 0.002243573 0.005357235 0.0000016   0.002665558
X   0.007467104 0.010856364 0.004896178 0.008258422 0.003086705 0.00341522  0.00000285  0.002702047
Y   0.007670113 0.00940323  0.006406508 0.007372615 0.003627189 0.001986754 0.0000465   0.002585564
Y   0.004686043 0.00726605  0.003055345 0.005017251 0.002003367 0.003900496 0.000000476 0.002309116
Y   0.004177442 0.006972389 0.002680084 0.00474762  0.001885973 0.004097005 0.000000662 0.002295276
Y   0.004475911 0.006846188 0.003625686 0.004221453 0.002829967 0.002831975 0.00000357  0.001636054
Y   0.005848798 0.008090652 0.00340937  0.006071063 0.003104326 0.003234079 0.000000615 0.003722661
Y   0.006518662 0.009792935 0.004230156 0.007402922 0.002677034 0.002810624 0.000000494 0.002678868
Y   0.005428432 0.008254325 0.003481542 0.006025152 0.002306644 0.002731699 0.0000341   0.002559538
Y   0.006138387 0.007997736 0.003884782 0.005445355 0.003097547 0.003177233 0.0000197   0.003667534
Y   0.005268127 0.006967856 0.003245438 0.004784031 0.002785673 0.003147425 0.0000161   0.003360895
Y   0.006824798 0.008643139 0.00441859  0.006040691 0.003519989 0.003252144 0.0000123   0.003866648
Y   0.006756669 0.008882524 0.004407434 0.006087051 0.003316964 0.0038456   0.00000907  0.003794542
Y   0.006766808 0.008916508 0.004425926 0.006133224 0.003349647 0.00381514  0.0000122   0.003794175
Y   0.005288403 0.006397465 0.003993784 0.004615067 0.002988219 0.00219577  0.000224752 0.002179541
Y   0.007064576 0.010323695 0.00479257  0.007002344 0.003084699 0.005404821 0.00000601  0.003126432
Y   0.004992499 0.007830614 0.003199636 0.005100254 0.002196152 0.004706792 0.0000291   0.002574521
Y   0.007285894 0.009257734 0.004756352 0.006956334 0.003740338 0.002541691 0.0000017   0.003785755
Y   0.007284548 0.009060781 0.004695172 0.006907381 0.003923092 0.00237505  0.00000764  0.004050348
Y   0.00502541  0.006910243 0.00368707  0.004952021 0.003261628 0.002949114 0.000131343 0.002590292
Y   0.005605946 0.008788954 0.003741203 0.006023171 0.002442795 0.004465909 0.000000421 0.002661231
Y   0.003618486 0.006465109 0.002340945 0.004184876 0.001724263 0.003914174 0.00000106  0.002229038
Y   0.004858636 0.008062734 0.00315832  0.005736985 0.002185119 0.003723115 0.00000306  0.002509759
Z   0.005107876 0.008322495 0.003352967 0.005921168 0.002309924 0.003758071 0.00000206  0.002624568
Z   0.003509392 0.005521583 0.002485114 0.003173729 0.00126778  0.00355057  0.0000431   0.001887588
Z   0.003865434 0.005767412 0.002810001 0.003372551 0.001460574 0.003648029 0.0000457   0.001909999
Z   0.005177201 0.007826825 0.003958008 0.004716213 0.002546243 0.00398816  0.00000473  0.00242293
Z   0.006601694 0.009499528 0.004681797 0.00669723  0.003016417 0.004643877 0.00000777  0.003081804

Then, I used down below to create a figure as you can see in attachment (Figure1).

library(reshape2)
library(ggplot)
DvsO = read.table("Disease_vs_Outcome.txt", header = TRUE)
DvsO  <- data.frame(DvsO)
DvsO <- melt(DvsO, id.vars="Disease")


ggplot(DvsO, aes(Disease, value, fill=Disease)) + 
  geom_violin() + geom_boxplot(width=.5) + facet_wrap(~variable,scales="free") +  scale_fill_manual(values=c("#CC6666", "#00CC00","#3399FF"))

However, I'd like to generate a figure as this one (Figure2). Basically, I'd like to flip the figure into horizontal and put all the data together in one panel.

I'd be happy if you help me to figure out this? Thank you very much in advance.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
Lothlorian
  • 55
  • 2
  • 9

1 Answers1

1

You can flip the boxes with coord_flip (but you can't use free scales with that). I would advise to keep the facets, but use facet_grid instead. Making one panel is much harder. You can adjust the look of the facets if needed.

(You can probably do away with the y-axis in this plot.)

ggplot(DvsO, aes(Disease, value, fill=Disease)) + 
  geom_violin() + 
  geom_boxplot(width=.5) + 
  facet_grid(variable~.) +
  scale_fill_manual(values=c("#CC6666", "#00CC00","#3399FF")) +
  coord_flip() + 
  theme_bw() +
  theme(panel.margin = unit(0, "lines"))

enter image description here

EDIT:

And with points (you could also look at geom_dotplot):

ggplot(DvsO, aes(Disease, value, fill = Disease)) + 
  geom_violin(color = NA, alpha = 0.8) + 
  geom_point(position = position_jitter(w = 0.5, h = 0), alpha = 0.7) +
  facet_grid(variable~.) +
  scale_fill_manual(values=c("#CC6666", "#00CC00","#3399FF")) +
  coord_flip() + 
  theme_bw() +
  theme(panel.margin = unit(0, "lines"), 
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank())

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • Hey Axeman! Thanks a lot! It works amazingly. I appreciate your help! Cheers, Bahti! – Lothlorian Apr 26 '16 at 15:32
  • Can I ask one more thing? If I'd like to add all the values as a dot into each violin plot, would that be possible? We can erase the box plot from inside and add the values on top the violin plot. Is that easy to do? Thanks a lot. – Lothlorian Apr 26 '16 at 15:36
  • Yeah, not very difficult. – Axeman Apr 26 '16 at 16:21
  • Thank you very much! I managed to do a graph that I like. Thanks for your time and help. – Lothlorian Apr 26 '16 at 17:06