wastelander
wastelander

Reputation: 15

Plotting aggregate data in R

I have a set of data that consists of 358 rock samples and their densities, p-wave velocities, and rock type (a category from 1-8).

I used the aggregate function to find the mean density for each rock type and mean p-wave velocity for each rock type.

dens = aggregate(SONIC_TXT$Density_g.cm3, by=list(SONIC_TXT$Rock_type), mean)
vel = aggregate(SONIC_TXT$P.WaveVelocity_km.sec, by=list(SONIC_TXT$Rock_type), mean)

I now want to plot the results of these on a scatterplot of the mean density of each rock type vs. mean p-wave velocity of each rock type.

My attempts were returned with errors saying I needed "finite limits". So I added limits to the plot function and got a plot with those limits and no points. I tried ggplot and got no error, but also just a "plot" with no axes/borders/points/anything at all.

Can anyone help me?


Edit: My attempts consisted of:

  1. plot(dens$vel)

    Which resulted in:

        Error in plot.window(...) : need finite 'xlim' values
    In addition: Warning messages:
    1: In min(x) : no non-missing arguments to min; returning Inf
    2: In max(x) : no non-missing arguments to max; returning -Inf
    3: In min(x) : no non-missing arguments to min; returning Inf
    4: In max(x) : no non-missing arguments to max; returning -Inf
    
  2. plot(dens$vel, xlim = c(0, 50), ylim = c(0,50),ylab ='ylab', xlab='xlab')

    Which resulted in:

    This plot

  3. matplot(dens, cbind(dens$x,vel$x),type="p",col=c("red","green"),pch=16)

    Which resulted in: This plot, which is more along the lines of what I was aiming for. Except for the velocity values don't line up with rock type now.

    enter image description here


Edit 2:

> dens2
  Rock_type Density_g.cm3
1         1      2.633226
2         2      2.677167
3         3      2.774167
4         4      2.919500
5         5      2.823643
6         6      2.794964
7         7      3.006226
8         8      3.240798

> vel2
  Rock_type P.WaveVelocity_km.sec
1         1              5.640581
2         2              5.803310
3         3              5.691533
4         4              6.426667
5         5              5.828643
6         6              6.217643
7         7              6.715594
8         8              7.556798

Upvotes: 1

Views: 7590

Answers (1)

gung - Reinstate Monica
gung - Reinstate Monica

Reputation: 11893

In your data, there is no vel in dens2, and dens2 is a data frame, not a vector. Try this:

d <- data.frame(dens=dens2$Density_g.cm3, vel2=vel2$P.WaveVelocity_km.sec, 
                type=dens2$Rock_type)
windows()
  with(d, plot(dens, vel2, pch=as.character(type)))

enter image description here


(See revision history for original answer, which was speculation, and is outdated now that there is a reproducible example.)

Upvotes: 1

Related Questions