adda.fuentes adda.fuentes - 3 years ago 209
R Question

Showing only one variable in R barplot

I have two binary variables consisting of 0 and 1 value. I want to create a barplot of the two variable that only shows the count of value equal to 1. I created the following code:

counts = table(Flag1, Flag2)
barplot(counts, beside = TRUE)

this code gives me the following plot:
enter image description here

I am trying to create a plot similar to this one but that only shows the two barplots for the value 1 instead of both 0 and 1 barplots. Is there any way to achieve this in R?

Any suggestions or tips would be greatly appreciated!

Answer Source

You can subset counts before sending to barplot.

counts <- as.matrix(table(mtcars$vs, mtcars$am)[, 2])

Note I've used as.matrix because it would drop to a vector without and that is what controls the standard palette.

barplot(counts, beside = TRUE)

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download