Escher
Escher

Reputation: 5776

R - how to make barplot plot zeros for missing values over the data range?

Lets say I have 10 observations of 200 points of integers between one and ten:

mysample = sample(rep(seq(1,10),20),10);

and I want to barplot it

barplot(table(mysample));

barplot enter image description here

In this example, there are no observations of 7. Is there a quick way of telling barplot to set the x-axis range to all integers between 1 and 10, or do I have to manually edit the table?

Upvotes: 11

Views: 5093

Answers (1)

MrFlick
MrFlick

Reputation: 206242

Try

barplot(table(factor(mysample, levels=1:10)));

enter image description here

By using a factor, R will know which levels are "missing"

Upvotes: 15

Related Questions