Reputation: 27
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))
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.
Thank you in advance for your help.
Upvotes: 1
Views: 3668
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()
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()
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
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)
Upvotes: 3