0

I have something like the following Dataset:

1. Donation 2. Treatment 3. Category. 
2€              TG2           Teenager
1€              TG1           Adult
2€              TG2           Adult
and so on

There are multiple participants. Every Participant is part of a special Treatmentgroup (TG) and is assigned to a category.

I would now like to plot two barplots like this:

  1. Donations of all TGs in one diagram
  2. Donations of all Categories in one diagram

I'm not sure how to do that. If i use barplot(table(Donation)), it gives me the barplot of the donations only. Can I somehow group this data in a barplot?

For example

Anonymosaurus
  • 99
  • 1
  • 9
  • It's easier to help you if you provide your sample data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Do you want to plot the count of donations or the sum of the donations? – MrFlick Feb 03 '20 at 15:49
  • Hi MrFlick, unfortunately my dataset is huge that's why I tried to break it down to a very simple example. I want to plot the sum of donations – Anonymosaurus Feb 03 '20 at 16:08
  • You can put a small example of it by following guidelines of the link provided by @MrFlick. – dc37 Feb 03 '20 at 17:02

1 Answers1

1

Without a reproducible example, it is difficult to be sure that this answer will fit to your dataset.

Here, based on few informations you provided, I generate a fake example:

df <- data.frame(Donation = sample(1:20, 100, replace = TRUE),
                 Treatment = sample(c("TG1","TG2"), 100, replace = TRUE),
                 Category = sample(c("Teenager","Adult"), size = 100, replace = TRUE))

  Donation Treatment Category
1        2       TG2    Adult
2        6       TG2    Adult
3       11       TG1 Teenager
4       16       TG2    Adult
5        7       TG1 Teenager
6       17       TG2    Adult

You can use dplyr to compute the sum of donation per treatment group and category as follow:

library(dplyr)
DF <- df %>% group_by(Treatment, Category) %>% 
  summarise(Sum = sum(Donation)) 

# A tibble: 4 x 3
# Groups:   Treatment [2]
  Treatment Category   Sum
  <fct>     <fct>    <int>
1 TG1       Adult      207
2 TG1       Teenager   236
3 TG2       Adult      372
4 TG2       Teenager   235

Then, you can use this new dataframe "DF" to get bargraph using ggplot2:

library(ggplot2)
ggplot(DF, aes(x = Treatment, y = Sum, fill = Category))+
  geom_col(position = position_dodge())

enter image description here

EDIT: Separating bargraph

To get only the sum of Donation per treatment group:

ggplot(DF, aes(x = Treatment, y = Sum, fill = Treatment))+
  geom_col()

enter image description here

To get only the sum of Donation per category:

ggplot(DF, aes(x = Category, y = Sum, fill = Category))+
  geom_col()

enter image description here

dc37
  • 15,840
  • 4
  • 15
  • 32
  • Hi dc37, first of all thank you very much for your help, looks good! However, I would like to have two different plots with 1. sum on y axis and Treatments on x axis and 2. sum on y axis and Category on x axis. Is this possible too? – Anonymosaurus Feb 03 '20 at 17:35
  • Yes it is possible. I edited my answer to show you how to do it. – dc37 Feb 03 '20 at 17:45