0

I'm attempting to plot selected samples from the larger group of samples I have in a data frame in R.

The data frame is constructed as follows:

round1Counts <- data.frame(
  Sample = c(round1[1:20,1], "LowerLimit", "UpperLimit"),
  Day = c(rep("1",22), rep("3",22), rep("6",22), rep("8",22)),
  Counts = c(strtoi(round1[1:20,2]), 50000, 240000, strtoi(round1[1:20,3]), 50000, 240000, strtoi(round1[1:20,4]), 50000, 240000, strtoi(round1[1:20,5]), 50000, 240000)
)
round1Counts$Sample <- as.character(round1Counts$Sample)
round1Counts$Sample <- factor(round1Counts$Sample, levels=c(round1[,1],"LowerLimit","UpperLimit"))

round1 is a 20 by 5 matrix, where the first column is a list of ids and the following 4 are different time points.

My strategy so far works when including all samples but only selecting certain timepoints,

# All samples, days 1 & 3
ggplot(data = round1Counts[1:44,], aes(x = Day[1:44], y = Counts[1:44], group = Sample[1:44], color = Sample[1:44])) +
  geom_line() +
  theme(panel.background = element_rect(fill = "white", colour = "grey50"))

but fails when I attempt to isolate certain samples,

# First 5 samples, days 1 & 3
ggplot(data = round1Counts[c(1:5,23:27),], aes(x = Day[c(1:5,23:27)], y = Counts[c(1:5,23:27)], group = Sample[c(1:5,23:27)], color = Sample[c(1:5,23:27)])) +
  geom_line() +
  theme(panel.background = element_rect(fill = "white", colour = "grey50"))

generating the following error:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
Warning message:
Removed 5 row(s) containing missing values (geom_path).

The function does not seem to recognize that round1Counts[1,] and round1Counts[23,] refer to the same sample, but I would expect grouping by Sample to allow recognition, as the same value is located at both of these indices.

  • See the answer on this post [here](https://stackoverflow.com/questions/27082601/ggplot2-line-chart-gives-geom-path-each-group-consist-of-only-one-observation). You need to add `group = 1` into the `ggplot` or `geom_line` `aes()` – C.Robin Mar 24 '23 at 15:15
  • Does this answer your question? [ggplot2 line chart gives "geom\_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"](https://stackoverflow.com/questions/27082601/ggplot2-line-chart-gives-geom-path-each-group-consist-of-only-one-observation) – C.Robin Mar 24 '23 at 15:16

1 Answers1

0

You are already sampling rows with the data=round1Counts[c(1:5, 23:27),]. The terms in aes() should only be the column names and not a reference to the actual data vectors. What you specify in data=... is already sufficient enough to let ggplot know that you want to include rows 1 through 5 and 23 through 27.

Consequently, this should work instead (reformatted for clarity):

ggplot(
  data = round1Counts[c(1:5,23:27),],  # already samples your rows for you
  aes(
    x = Day,
    y = Counts,
    group = Sample,
    color = Sample  # kind of redundant here
  )
) +
  geom_line() +
  theme(
    panel.background = element_rect(fill = "white", colour = "grey50")
  )
chemdork123
  • 12,369
  • 2
  • 16
  • 32