OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Create two barplots for categorical variables having different numbers of classes [closed]

  • Thread starter Thread starter DeltaIV
  • Start date Start date
D

DeltaIV

Guest
I have a dataframe:

Code:
                                          Variable Class  Probability
0                                                0     0     0.2
1                                                0     1     0.8
2                                                1     0     0.34
3                                                1     1     0.66
4                                               10     0     0.5
5                                               10     1     0.5
34                 VerylongAndAnnoyingVariableName    no     0.94
35                 VerylongAndAnnoyingVariableName   yes     0.06
36           EvenlongerAndMoreAnnoyingVariableName     1     0.01
37           EvenlongerAndMoreAnnoyingVariableName     2     0.1
38           EvenlongerAndMoreAnnoyingVariableName     5     0.1
39           EvenlongerAndMoreAnnoyingVariableName     7     0.79

where Variable is the variable name (some variables have integers as names...don't ask), Class are all the possible classes for the given Variable, and Probability is the probability of each class. I would like to create two different barplots:

  • in the first one, for each variable I want to stack n bars (one for class) on top of each other, with a different color. This way, for each variable I will have a bar of height 1 (since the sum of the class probabilities for each variables must be 1)
  • in the second one, I would like to have the bars corresponding to each class one next to the other, instead that stacked one on top of each other.

How can I build these two barplots?
<p>I have a dataframe:</p>
<pre><code> Variable Class Probability
0 0 0 0.2
1 0 1 0.8
2 1 0 0.34
3 1 1 0.66
4 10 0 0.5
5 10 1 0.5
34 VerylongAndAnnoyingVariableName no 0.94
35 VerylongAndAnnoyingVariableName yes 0.06
36 EvenlongerAndMoreAnnoyingVariableName 1 0.01
37 EvenlongerAndMoreAnnoyingVariableName 2 0.1
38 EvenlongerAndMoreAnnoyingVariableName 5 0.1
39 EvenlongerAndMoreAnnoyingVariableName 7 0.79
</code></pre>
<p>where <code>Variable</code> is the variable name (some variables have integers as names...don't ask), <code>Class</code> are all the possible classes for the given <code>Variable</code>, and <code>Probability</code> is the probability of each class. I would like to create two different barplots:</p>
<ul>
<li>in the first one, for each variable I want to stack n bars (one for class) on top of each other, with a different color. This way, for each variable I will have a bar of height 1 (since the sum of the class probabilities for each variables must be 1)</li>
<li>in the second one, I would like to have the bars corresponding to each class one next to the other, instead that stacked one on top of each other.</li>
</ul>
<p>How can I build these two barplots?</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top