Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binning data, finding results by group, and plotting using R

Tags:

r

The pre-installed quakes dataset has 5 variables and 1000 observations.

The simple graph I'm trying to create should show the average earthquake magnitude by earthquake depth category (i.e. Y-axis = Magnitude , X-axis = Depth Categories).

In this dataset, the earthquake depth variables range from 40 to 680. I would like to turn the 1000 observations of earthquake depth into 8 categories, e.g. 40 - 120, 121 - 200, ... 600 - 680. Then, I'd like to take the average earthquake magnitude by depth category and plot it on a line chart.

I appreciate any help with this. Thanks!

like image 939
AME Avatar asked Dec 02 '22 03:12

AME


1 Answers

First classify into depth classes with cut:

depth.class <- cut(quakes$depth, c(40, 120, 200, 300, 400, 500, 600, 680), include.lowest = TRUE)

(Note that your class definitions may need to vary for exactly what you are after and given the details of cut()'s behaviour).

Find the mean magnitude within each depth.class (assumes no NAs):

mean.mag <- tapply(quake$mag, depth.class, mean)

(Add na.rm e.g. mean.mag <- tapply(quake$mag, depth.class, mean, na.rm = TRUE) for data sets with missing values where appropriate).

Plot as a line:

plot(mean.mag, type = "l", xlab = "magnitude class")

It's a little extra work to put the class labels on the X-axis, but at that point you might question if a line plot is really appropriate here.

A quick stab, turn off the axes and then put up the classes directly from the cut factor:

plot(mean.mag, type = "l", xlab = "magnitude class", axes = FALSE)
axis(1, 1:nlevels(depth.class), levels(depth.class))
axis(2)
box()
like image 68
mdsumner Avatar answered Dec 23 '22 14:12

mdsumner