Oscar Silveiro
Oscar Silveiro

Reputation: 35

Represent a graph

I have the next data, with dput(head(titanic7,20))

structure(list(PassengerId = 1:20, Survived = c("NO", "YES", 
"YES", "YES", "NO", "NO", "NO", "NO", "YES", "YES", "YES", "YES", 
"NO", "NO", "NO", "YES", "NO", "YES", "NO", "YES"), Pclass = c(3L, 
1L, 3L, 1L, 3L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 2L, 3L, 
2L, 3L, 3L), Name = c("Braund, Mr. Owen Harris", "Cumings, Mrs. John Bradley (Florence Briggs Thayer)", 
"Heikkinen, Miss. Laina", "Futrelle, Mrs. Jacques Heath (Lily May Peel)", 
"Allen, Mr. William Henry", "Moran, Mr. James", "McCarthy, Mr. Timothy J", 
"Palsson, Master. Gosta Leonard", "Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)", 
"Nasser, Mrs. Nicholas (Adele Achem)", "Sandstrom, Miss. Marguerite Rut", 
"Bonnell, Miss. Elizabeth", "Saundercock, Mr. William Henry", 
"Andersson, Mr. Anders Johan", "Vestrom, Miss. Hulda Amanda Adolfina", 
"Hewlett, Mrs. (Mary D Kingcome)", "Rice, Master. Eugene", "Williams, Mr. Charles Eugene", 
"Vander Planke, Mrs. Julius (Emelia Maria Vandemoortele)", "Masselmani, Mrs. Fatima"
), Sex = c("male", "female", "female", "female", "male", "male", 
"male", "male", "female", "female", "female", "female", "male", 
"male", "female", "female", "male", "male", "female", "female"
), Age = c(22, 38, 26, 35, 35, 26, 54, 2, 27, 14, 4, 58, 20, 
39, 14, 55, 2, 31, 31, 31), SibSp = c(1L, 1L, 0L, 1L, 0L, 0L, 
0L, 3L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 4L, 0L, 1L, 0L), Parch = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 1L, 0L, 0L, 5L, 0L, 0L, 1L, 
0L, 0L, 0L), Ticket = c("A/5 21171", "PC 17599", "STON/O2. 3101282", 
"113803", "373450", "330877", "17463", "349909", "347742", "237736", 
"PP 9549", "113783", "A/5. 2151", "347082", "350406", "248706", 
"382652", "244373", "345763", "2649"), Fare = c(7.25, 71.2833, 
7.925, 53.1, 8.05, 8.4583, 51.8625, 21.075, 11.1333, 30.0708, 
16.7, 26.55, 8.05, 31.275, 7.8542, 16, 29.125, 13, 18, 7.225), 
    Embarked = c("S", "C", "S", "S", "S", "Q", "S", "S", "S", 
    "C", "S", "S", "S", "S", "S", "S", "Q", "S", "S", "C"), Title = c("Mr", 
    "Mrs", "Miss", "Mrs", "Mr", "Mr", "Mr", "Master", "Mrs", 
    "Mrs", "Miss", "Miss", "Mr", "Mr", "Miss", "Mrs", "Master", 
    "Mr", "Mrs", "Mrs"), agecat = structure(c(3L, 4L, 3L, 4L, 
    4L, 3L, 6L, 1L, 3L, 2L, 1L, 6L, 2L, 4L, 2L, 6L, 1L, 4L, 4L, 
    4L), .Label = c("<10", "10-20", "20-30", "30-40", "40-50", 
    "50-60", "60-70", ">70"), class = "factor"), Familysize = c(1L, 
    1L, 0L, 1L, 0L, 0L, 0L, 4L, 2L, 1L, 2L, 0L, 0L, 6L, 0L, 0L, 
    5L, 0L, 1L, 0L), Sigleton = c(TRUE, TRUE, FALSE, TRUE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, TRUE, FALSE)), row.names = c(NA, 
-20L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = c("SibSp", 
"Parch"), drop = TRUE, indices = list(c(2L, 4L, 5L, 6L, 11L, 
12L, 14L, 15L, 17L, 19L), 8L, c(0L, 1L, 3L, 9L, 18L), 10L, 13L, 
    7L, 16L), group_sizes = c(10L, 1L, 5L, 1L, 1L, 1L, 1L), biggest_group_size = 10L, labels = structure(list(
    SibSp = c(0L, 0L, 1L, 1L, 1L, 3L, 4L), Parch = c(0L, 2L, 
    0L, 1L, 5L, 1L, 1L)), row.names = c(NA, -7L), class = "data.frame", vars = c("SibSp", 
"Parch"), drop = TRUE))

When making bar graphs with the number of passengers that have survived for each "SEX" and "Pclass" value, I did so:

 titanic7 %>% ggplot() + geom_bar(aes(Sex, fill = Survived))
 titanic7 %>% ggplot() + geom_bar(aes(Pclass, fill = Survived))

But is there any way to do it using a single graphic?

Thanks!!

Upvotes: 0

Views: 51

Answers (1)

Thiago Fernandes
Thiago Fernandes

Reputation: 273

Use the facet_grid

titanic7 %>% ggplot() + geom_bar(aes(Sex, fill = Survived)) + facet_grid(.~ Pclass)

titanic sex and survived count

Upvotes: 1

Related Questions