rggplot2survival-analysis

Plot distribution of follow-up times according to event status


I want to create a plot of the distribution of follow-up times of variable futime on x-axis according to event status in variable fustat on y-axis.

data(cancer, package="survival")

My attempt:

library(ggplot2)
ggplot(ovarian, aes(x = futime, fill = factor(fustat))) +
  geom_histogram(binwidth = 50) +
  labs(x = "Futime", y = "Count", fill = "Fustat") +
  scale_fill_manual(values = c("0" = "red", "1" = "blue")) +
  theme_minimal()

But how to get it more nicely like this

Thanks


Solution

  • Example with a built-in dataset as we don't have your data:

    library(ggplot2)
    ggplot(iris, aes(x = Sepal.Length, fill = factor(Species))) +
      geom_histogram(binwidth = 0.2, alpha = 0.4, position = "identity")
    

    gives: enter image description here