SamSom
SamSom

Reputation: 15

Barplot/Plot with empty data value

In my data, Week_death represent the week a patient dies. Some weeks none of the patients are dying.

I am trying to do a barplot of mortality of patients. I would like to show the week without death with an empty barplot.

death <- table(as.numeric(data$Week_death)) 

barplot(death)

The result is the following

enter image description here

I tried also

plot(death)

and it gave me this result:

enter image description here

How do I add empty weeks?

Thanks in advance

Upvotes: 1

Views: 70

Answers (1)

Skaqqs
Skaqqs

Reputation: 4140

Make your weeks variable a factor and set its levels. table() will retain counts of 0.

# Dummy dataset
data <- data.frame(
  week = sample(32:50, 20, replace=TRUE))

# Make week a factor and set levels
data$week <- factor(data$week, levels=c(32:50))

# Get frequencies
# Note that empty factor levels are retained
death <- table(data)
death
#> data
#> 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 
#>  2  2  1  2  1  0  2  0  1  0  1  1  1  2  0  2  1  0  1
barplot(death)

enter image description here

Upvotes: 1

Related Questions