r - Plots not showing in ggplot2 with geom_bar -


i trying plot stacked bars using ggplot() , geom_bar()

sample data (titanic kaggle question):

passengerid   survived   age           1          0    25           2          1    20           3          1    40           4          0    10 

i trying show stacked bars of survival , death each age range (i have divided age bins). plot not visible when execute command. , when add print() function, error

error: no layers in plot

please tell if there missing out here ?

breaks <- seq(min(train$age), max(train$age), 10) p <- ggplot(train, aes(x=train$age, y=length(train$passengerid)), xlab = "age", ylab = "count", main = "survival",     fill = survived + geom_bar(stat = "identity", bin = breaks))  print(p) 

"train" object in stored data.

apart pascal's hint, may want to:

  • use factors colored column (survived)
  • use different geom_bar() , aes() params suggested

code:

train <- data.frame(passengerid = 1:4, survived = factor(c(0,1,1,0)), age = c(25, 20, 40, 10)) bin.width <- 10 my.breaks <- seq(from = min(train$age), = max(train$age) + bin.width, = bin.width) ggplot(train, aes(age, fill = survived)) + geom_bar(breaks = my.breaks) 

plot:

stacked chart produced


Comments