Showing posts with label barplot(). Show all posts
Showing posts with label barplot(). Show all posts

Saturday, March 5, 2011

ploting bar graphs with plot()


Its easy to make histograms with hist() to show you the distribution of a set of data.

For actual bar plot, such as you would make in excel, there's also a function called barplot(). I think you have more control, however, you you use the regular function plot() with a few modifications.

I work with a population of plants that often go dormant. I had a proportion table like the output below for 8 years of data. Some plants were around all 8 years, but many went dormant for 1 or more years.

I made the proportion table using table() and prop.table(); round() gets rid of the excess decimal places.
> round(prop.table(table(factor(S_tot_yrs_pres_03_10))),3)

The table output is like this:
0 1 2 3 4 5 6 7 8
0.009 0.120 0.098 0.079 0.110 0.144 0.180 0.140 0.120

This give me the portion of plants that grew a certain number of years. I grabbed this data and the plotted it as a bar graph ("column" graph in Excel parlance) using plot() with a few extra calls.

The code was this:
plot(years_above_ground, portion_of_pop, type="h", lwd=50, lend=2, col=2, ylim=c(0,.25), xlab="years of growth", ylab="portion of pop")

The key to making this look like a bar graph are these three pieces
type="h", #This tell it to make a graph of type "histogram"
lwd=50, #This tell the line width to be 50; otherwise it will plot skinny lines.
lend=2, #this makes the line end style to be flat; the default is rounded.