0

This question is not about barplot directly. It is about how to prepare the date to create a stacked barplot.

This is the dataframe with three columns

   K    nA    nB
1  A   852  1242
2  B   520  1660
3  C  1014  3513
4  D  1642  2954
5  E  6001 11357
6  F  1790  8120
7  G  3823 10937
8  H   518  7217
9  I  8145 21124
10 J  4948 10707
11 K  2224  6197
12 L  1192  4852
13 M  1370  9575
14 N  2990  7636
15 O     0     9
16 P    10    79
17 Q   133   914
18 R  7303 13894
19 S   535  1678
20 T  1104  2711
21 U   903   847
22 X     0     5
23 Y     0    18
24 Z 10061 14321

I can easly do

barplot(df$nA)

To draw a simple barplot. But my desired output is a stacked barplot. The letters are the bars. Each bar should have two stacks (nA and nB). I know I have to transform the data into a matrix or table.

But I don't know how. I don't understand the logic behind it. I found a lot of ressources about that topic on the web. But I can not get into it.

How can I transform this data?

buhtz
  • 10,774
  • 18
  • 76
  • 149
  • 1
    So I wonder if [you tried Googling](http://stackoverflow.com/questions/20349929/stacked-bar-plot-in-r) prior posting... – David Arenburg Aug 19 '16 at 07:54
  • I see I didn't formulated my question specific enough. I found that "duplicate" and othere ressources about it before. – buhtz Aug 19 '16 at 07:56
  • 1
    Does `barplot(as.matrix(df[-1]))` gives you what you want? What is the desired output? – David Arenburg Aug 19 '16 at 07:59
  • @DavidArenburg I edited the question to described the desired output. Your solution produce two bars with 24 stacks. I want the transposed one: 24 bars each two stacks. – buhtz Aug 19 '16 at 08:01
  • 1
    `barplot(t(df[-1]))` then? – David Arenburg Aug 19 '16 at 08:02
  • I really want to understand what is going on. So I can use that understanding with other data, too. ;) – buhtz Aug 19 '16 at 08:03
  • 1
    @DavidArenburg `t(df[-1])` does the thing. Thank you. And I think I understand what happen here in that example. Let's see in the future if I can use that on other data, too. ;) – buhtz Aug 19 '16 at 08:06
  • @DavidArenburg This isn't a duplicate. My question is about transforming data not about plotting. The question you mentioned is about plotting. – buhtz Aug 19 '16 at 13:46

0 Answers0