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!
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With