0

Im new to R and im tring to create an histogram.

my data looks like this:

                id age                               sub.types inter
1 TCGA-A8-A07O  60        Infiltrating duct carcinoma, NOS     6
2 TCGA-A7-A0CJ  65        Infiltrating duct carcinoma, NOS     6
3 TCGA-A7-A0CH  88        Infiltrating duct carcinoma, NOS     8
4 TCGA-A7-A0CG  86 Infiltrating duct and lobular carcinoma     8
5 TCGA-A7-A0CE  66        Infiltrating duct carcinoma, NOS     6
6 TCGA-A7-A0CD  75        Infiltrating duct carcinoma, NOS     7

i want to create an histogram so that for every groupe age (30-39, 40-49 etc) i will get a bar devided and colored by sub types.

i maneged to create such a plot using ggplot for each groupe sepratly but not together (by using filter)

can any one derect me to the right answer?

atsyplenkov
  • 1,158
  • 13
  • 25
noam
  • 7
  • 1

1 Answers1

0

You can use cut to group by range. Read more about it here. Below is an example how to plot your data using ggplot2 and dplyr. I've grouped by 10 years.

library(ggplot2)
library(dplyr)

df <- tibble::tribble(
                   ~id, ~age,                                ~sub.types, ~inter,
        "TCGA-A8-A07O",  60L,        "Infiltrating duct carcinoma, NOS",     6L,
        "TCGA-A7-A0CJ",  65L,        "Infiltrating duct carcinoma, NOS",     6L,
        "TCGA-A7-A0CH",  88L,        "Infiltrating duct carcinoma, NOS",     8L,
        "TCGA-A7-A0CG",  86L, "Infiltrating duct and lobular carcinoma",     8L,
        "TCGA-A7-A0CE",  66L,        "Infiltrating duct carcinoma, NOS",     6L,
        "TCGA-A7-A0CD",  75L,        "Infiltrating duct carcinoma, NOS",     7L
        )

df %>% 
  group_by(age.group = cut(age, breaks= seq(0, 100, by = 10))) %>% 
  ggplot() +
  geom_bar(aes(age.group, fill = sub.types)) +
  theme_classic()

enter image description here

atsyplenkov
  • 1,158
  • 13
  • 25
  • Tnx! but i need the bars to be side by side and not on each other, any ideas? – noam Nov 28 '18 at 17:44
  • @noam just add `position = "dodge"` to `geom_bar()`. Take a look in the reference: https://ggplot2.tidyverse.org/reference/position_dodge.html – atsyplenkov Nov 28 '18 at 17:57