I am a little confused by your data... I am assuming from your example that car 1 has 101 dents that did not remove paint and 9 that did and car 2 has 131 that did not and 19 that did.
Now calculating the standard deviation on the number of dents does not make much sense to me... you are plotting count data, so you should not have any standard deviation unless you have, say, many cars of the same model and you want to see the variability between cars.
The best thing to do would be to calculate the % of dents that removed paint by doing:
car1yr1 = c(rep(0, 101), rep(1, 9)) #car has 9 dents that remove paint
car1yr5 = c(rep(0, 131), rep(1, 19)) #car has 19 dents that remove paint
# The total number of observations is the total number of dents
total.dents.1 <- length(car1yr1)
total.dents.5 <- length(car1yr5)
# The dents that remove paint are marked as 1, the others with 0,
# so we can just sum all of the data to get the number of paint-removing dents
dents.paint.1 <- sum(car1yr1)
dents.paint.5 <- sum(car1yr5)
# Alternatively you can use
# dents.paint.1 <- length(which(car1yr1==1))
# Calculate the %
dents.paint.perc.1 <- dents.paint.1/total.dents.1
dents.paint.perc.5 <- dents.paint.1/total.dents.5
df <- data.frame(dents.paint.perc.1, dents.paint.perc.5)
# Plot the data.
# ylim specifies the limits of the y axis
# ylab defines the axis title.
# las=1 puts the labels on the y axis horizontally
# names defines the labels on the x axis
barplot(as.matrix(df)*100, ylim=c(0,20),
ylab="% dents removing paint", las=1,
names=c("Car 1 year 1", "Car 1 year 5"))
In general it would be much better to put all your data in a single list, so that you can use the *apply
family of function to perform repetitive operations on all of your dataset. This will give you cleaner and more manageable code. Also, if you add more data it will automagically add it to the plot.