0
votes

I have three columns of data (transect name, x-distance from start of transect, elevation along transect) and would like to quickly make an x-y scatter plot where each series is based on the transect name. I want to compare the slopes of the transects.

The three columns of data look like this:

Transect, X-distance, Elevation
A, 0, 0
A, 1, 2
A, 1, 3
A, 2, 4
B, 0, 0
B, 1, 3
B, 2, 4
C, 0, 0
C, 1, 1

etc. with about 15 "letters/transects" worth of data. In total about 150-200 rows.

Note also that the number of points per letter is variable (between 8 and 15 or so) and that they don't share any common X or Y (in this case distance and elevation)

I've googled/searched the "similar questions" but none of the answers seem to have the data set up the same way as me. They either share the same X, or have the series in adjacent columns.

Is there a way for R to just recognize the different names in the first column and make them different series?

Thanks

1
Do you really want to make the plots or do you just want a list of the slopes by transect? - G5W
library(dplyr); df %>% group_by(Transect) %>% ...(whatever needs to happen next) - Ryan Morton
Hi G5W. Eventually, yes, I'll want the slopes (I'm fairly strong in Excel so can get it there easy enough). If there's a quicker way to output the slopes, based on the transect, then I'm all for learning it. - Dr. Peat

1 Answers

1
votes

I believe this is what you are trying to accomplish. You can group by transect using the setting color = Transect in your aesthetics. I added geom_line() just to help visualise the slope. Feel free to exclude it.

library(tidyverse)

df <- tribble(
~Transect, ~X_distance, ~Elevation,
'A', 0, 0,
'A', 1, 2,
'A', 2, 3,
'A', 3, 4,
'B', 0, 0,
'B', 1, 3,
'B', 2, 4,
'C', 0, 0,
'C', 1, 1)


ggplot(df, aes(X_distance, Elevation, color = Transect)) +
  geom_point() +
  geom_line()

enter image description here

If you wanted to set individual colors to certain groups while keeping all of the transects, add an additional column indicating the groupings. You can then add the group argument to your ggplot aesthetics.

df <- tribble(
  ~Transect, ~X_distance, ~Elevation, ~grouping,
  'A', 0, 0, 'AB',
  'A', 1, 2, 'AB',
  'A', 2, 3, 'AB',
  'A', 3, 4, 'AB',
  'B', 0, 0, 'AB',
  'B', 1, 3, 'AB',
  'B', 2, 4, 'AB',
  'C', 0, 0, 'C',
  'C', 1, 1, 'C')


ggplot(df, aes(X_distance, Elevation, group = Transect, color = grouping)) +
  geom_point() +
  geom_line()

enter image description here