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

前端 未结 4 2056
眼角桃花
眼角桃花 2020-12-10 08:23

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 earth

4条回答
  •  情话喂你
    2020-12-10 09:09

    Are you sure that you want a line plot here? I'm not sure that is the most appropriate plot to use here. Regardless, the trick here is to use cut to bin the data appropriately, and then use one of the many aggregation tools to find the average magnitude by those groups. Finally, we'll plot those aggregated values. I like the tools in ggplot2 and plyr for tasks like this:

    library(ggplot2)
    df <- quakes
    df$bins <- with(df, cut(depth, breaks = c(0,40, 120, 200, 280, 360, 440, 520, 600, 680)))
    df.plot <- ddply(df, .(bins), summarise, avg.mag = mean(mag))
    qplot(bins, avg.mag, data = df.plot)
    
    #If you want a line plot, here's one approach:
    qplot(as.numeric(bins), avg.mag, data = df.plot, geom = "line") + 
    xlim(levels(df.plot$bins))
    

提交回复
热议问题