Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

bar-chart

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?

like image 955
Escher Avatar asked Oct 01 '14 18:10

Escher


1 Answers

Try

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

enter image description here

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

like image 137
MrFlick Avatar answered Nov 07 '22 07:11

MrFlick