Home > Software engineering >  Barplot using ggplot2 for 4 variables
Barplot using ggplot2 for 4 variables

Time:03-20

I have data like this:

ID  height  S1  S2  S3
1   927 0.90695438  0.28872194  0.67114294
2   777 0.20981677  0.71783084  0.74498220
3   1659    0.35813799  0.92339744  0.44001698
4   174 0.44829914  0.67493949  0.11503942
5   1408    0.90642643  0.18593999  0.67564278
6   1454    0.38943930  0.34806716  0.73155952
7   2438    0.51745975  0.12351953  0.48398490
8   1114    0.12523909  0.10811622  0.17104804
9   1642    0.03014575  0.29795320  0.67584853
10  515 0.77180549  0.83819990  0.26298995
11  1877    0.32741508  0.99277109  0.34148083
12  2647    0.38947869  0.43713441  0.21024554
13  845 0.04105275  0.20256457  0.01631959
14  1198    0.36139663  0.96387150  0.37676288
15  2289    0.57097808  0.66038711  0.56230740
16  2009    0.68488024  0.29811683  0.67998461
17  618 0.97111675  0.11926219  0.74538877
18  1076    0.70195881  0.59975160  0.95007272
19  1082    0.01154550  0.12019055  0.16309071
20  2072    0.53553213  0.78843202  0.32475690
21  1610    0.83657146  0.36959607  0.13271604
22  2134    0.80686674  0.95632284  0.63729744
23  1617    0.08093264  0.91357666  0.33092961
24  2248    0.23890930  0.82333634  0.64907957
25  1263    0.96598986  0.31948216  0.30288836
26  518 0.03767233  0.87770033  0.07123327
27  2312    0.91640643  0.80035100  0.66239047
28  2646    0.72622658  0.61135664  0.75960356
29  1650    0.20077621  0.07242114  0.55336017
30  837 0.84020075  0.42158771  0.53927210
31  1467    0.39666235  0.34446560  0.84959232
32  2786    0.39270226  0.75173569  0.65322596
33  1049    0.47255689  0.21875132  0.95088576
34  2863    0.58365691  0.29213397  0.61722305
35  2087    0.35238717  0.35595337  0.49284063
36  2669    0.02847401  0.63196192  0.97600657
37  545 0.99508793  0.89253107  0.49034522
38  1890    0.95755846  0.74403278  0.65517230
39  2969    0.55165118  0.45722242  0.59880179
40  395 0.10195396  0.03609544  0.94756902
41  995 0.23791515  0.56851452  0.36801151
42  2596    0.86009766  0.43901589  0.87818701
43  2334    0.73826129  0.60048445  0.45487507
44  2483    0.49731226  0.95138276  0.49646702
45  1812    0.57992109  0.26943131  0.46061562
46  1476    0.01618339  0.65883839  0.61790820
47  2342    0.47212988  0.07647121  0.60414349
48  2653    0.04238973  0.07128521  0.78587960
49  627 0.46315442  0.37033152  0.55526847
50  925 0.62999477  0.29710220  0.76897834
51  995 0.67324929  0.55107827  0.40428567
52  600 0.08703467  0.36989059  0.51071981
53  711 0.14358380  0.84568953  0.52353644
54  828 0.90847850  0.62079070  0.99279921
55  1776    0.12253259  0.39914002  0.42964742
56  764 0.72886279  0.29966153  0.99601125
57  375 0.95037718  0.38111984  0.78660025
58  694 0.04335591  0.70113494  0.51591063
59  1795    0.01959930  0.94686529  0.50268797
60  638 0.19907246  0.77282832  0.91163748
61  1394    0.50508626  0.21955016  0.26441590
62  1943    0.92638876  0.71611036  0.17385687
63  2882    0.13840169  0.66421796  0.40033126
64  2031    0.16919458  0.70625020  0.53835738
65  1338    0.60662738  0.27962799  0.24496437
66  1077    0.81587669  0.71225050  0.37585096
67  1370    0.84338121  0.66094211  0.58025355
68  1339    0.78807719  0.04101269  0.20895531
69  739 0.01902087  0.06114149  0.80133001
70  2085    0.69808750  0.27976169  0.63880242
71  1240    0.81509312  0.30196772  0.73633076
72  987 0.56840006  0.95661083  0.43881241
73  1720    0.48006288  0.38981872  0.57981238
74  2901    0.16137012  0.37178879  0.25604401
75  1987    0.08925623  0.84314249  0.46371823
76  1876    0.16268237  0.84723500  0.16861486
77  2571    0.02672845  0.31933115  0.61389453
78  2325    0.70962948  0.13250605  0.95810262
79  2503    0.76101818  0.61710912  0.47819473
80  279 0.85747478  0.79130451  0.75115933
81  1381    0.43726582  0.33804871  0.02058322
82  1800    0.41713645  0.90544760  0.17096903
83  2760    0.58564949  0.19755671  0.63996650
84  2949    0.82496758  0.79408518  0.16497848
85  118 0.79313923  0.75460289  0.35472278
86  1736    0.32615257  0.91139485  0.18642647
87  2201    0.95793194  0.32268770  0.89765616
88  750 0.65301961  0.08616947  0.23778386
89  906 0.45867582  0.91120045  0.98494348
90  2202    0.60602188  0.95517383  0.02133074

I want to make a barplot using ggplot2 like this: enter image description here

In the above-mentioned dataset height should be on the y-axis and S1, S2, S3 should be representing colors of each sample. I have tried the base R function barplot which gave me the following output. Please give me any suggestion.

barplot(t(as.matrix(examp[,3:5])),col=rainbow(3))

enter image description here

CodePudding user response:

It's not clear to me exactly what you want to plot. You say you want height on the y axis, but the examples you show are all 'filled to the top', implying the same height for each ID. Also, it is not clear what the numbers associated with each sample represent. I am guessing they should be relative weightings for the bar heights.

Assuming you actually want a filled bar plot as in the examples, with the relative sizes of the bars dictated by the sample values, you can do:

library(tidyr)
library(dplyr)
library(ggplot2)

df %>%
  mutate(ID = reorder(ID, S3/(S3   S2   S1))) %>%
  pivot_longer(3:5, names_to = "Sample", values_to = "Value") %>%
  ggplot(aes(ID, Value * height, fill = Sample))  
  geom_col(position = "fill", color = NA)  
  labs(y = "Height")  
  theme_classic()  
  scale_fill_manual(values = c("red", "green", "blue"))

enter image description here


Alternative


df %>%
  arrange(order(height)) %>%
  group_by(height) %>%
  summarize(across(everything(), mean)) %>%
  pivot_longer(3:5, names_to = "Sample", values_to = "Value") %>%
  ggplot(aes(height, Value, fill = Sample, colour = Sample))  
  geom_smooth(method = loess, formula = y ~ x, linetype = 2, alpha = 0.2)  
  theme_bw()

enter image description here

  • Related