Ignacio A.D.
Ignacio A.D.

Reputation: 27

Connecting points from different groups in a categorical X axis with ggplot

I am trying to visulaize the change in Abundance of A, B and C between three Methods. A, B and C are also divided in two groups ("X" and "Y"). I am trying to plot these with ggplot and connect the observations from method to method, but I have not been able to. This is what I've done:

factor_1 <- c(rep(c("A", "B", "C"), times =6))
Abundance <- c(sample(x = 1:100, replace = T, size = 18))
factor_2 <- c(rep(c("X", "Y"), each = 3, times = 3))
factor_3 <- c(rep(c("Method 1", "Method 2", "Method 3"), each = 6))
datframe <- tibble(factor_1, factor_2, Abundance, factor_3)     

The first plot only connects the dots vertically in each Method.

datframe %>%
  ggplot(aes(x = factor_3, y = Abundance, color = factor_2))+
  geom_point() +
  geom_line()

When trying to Group by either factor_1 or factor_2, it seems to connect everything in a single line

datframe %>%
  ggplot(aes(x = factor_3, y = Abundance, color = factor_2))+
  geom_point() +
  geom_line(group = c(factor_2))

datframe %>%
  ggplot(aes(x = factor_3, y = Abundance, color = factor_2))+
  geom_point() +
  geom_line(group = c(factor_1))

Even if I plot only one row, R complains, saying "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?" and does not connect the dots.

datframe %>%
  filter(factor_1 == "A", factor_2 == "X") %>%
  ggplot(aes(x = factor_3, y = Abundance, color = factor_2))+
           geom_point() +
           geom_line()

I know that it can be done when the X axis is a continuous variable, but I have failed to see it with a categorical variable.

This is more or less what I want. It does not need even to be colored coded, since I could make two plots, one for "X" and another for "Y".

Thank you in advance for your help.

Upvotes: 1

Views: 3668

Answers (2)

Ian Campbell
Ian Campbell

Reputation: 24878

One approach is with interaction:

library(ggplot2)
datframe %>%
  ggplot(aes(x = factor_3, y = Abundance, color = factor_2, group = interaction(factor_1,factor_2)))+
  geom_point() + 
  geom_line()

enter image description here

You might also consider including a second visual aesthetic to differentiate factor_1.

datframe %>%
  ggplot(aes(x = factor_3, y = Abundance, color = factor_2, linetype = factor_1, group = interaction(factor_1,factor_2)))+
  geom_point() + 
  geom_line()

enter image description here

Data

set.seed(1)
datframe <- tibble(factor_1 = rep(c("A", "B", "C"), times =6),
                   Abundance = sample(x = 1:100, replace = T, size = 18),
                   factor_2 = rep(c("X", "Y"), each = 3, times = 3),
                   factor_3 = rep(c("Method 1", "Method 2", "Method 3"), each = 6))

Upvotes: 1

Neel Kamal
Neel Kamal

Reputation: 1076

is this what you are looking for??

datframe %>%
  ggplot(aes(x = factor_1, y = Abundance, color = factor_2, group = factor_2))+
  geom_point() +
  geom_line() +
  facet_wrap(~factor_3)

enter image description here

Upvotes: 3

Related Questions